from itertools import cycle
def sum_arr(smaller_array, larger_array):
new_a = []
for _ in zip(cycle(smaller_array), larger_array): # cycle(0, 1, 2) => 0 1 2 0 1 2 . . .
new_a.append(sum(_))
return new_a
a = [1,1,1,1,1,1,1]
b = [0,1,2]
if len(a) < len(b): # check which one is the smaller and to be repeated
new_array = sum_arr(a, b)
else:
new_array = sum_arr(b, a)
print(new_array) # [1, 2, 3, 1, 2, 3, 1]
5条答案
按热度按时间mkshixfv1#
产生
[1, 2, 3, 1, 2, 3, 1]
ckx4rj1h2#
您可以将zip与cycle结合使用:
t3irkdon3#
ss2ws0br4#
我希望这可以理解。
pwuypxnk5#