我正在尝试像我在这里找到的功能一样添加列表,但问题是我无法弄清楚如何添加超过特定数量的列表。我希望它添加任意数量的列表,所以m + n + o应该是[12,15,18]谢谢!
m = [1, 2, 3]
n = [4, 5, 6]
o = [7, 8, 9]
def zipper(a,b):
x = [a[i] + b[i] for i in range(len(a))]
print x
print zipper(m,n)
答案 0 :(得分:3)
您可以使用zip
:
In [1]: m = [1, 2, 3]
...: n = [4, 5, 6]
...: o = [7, 8, 9]
...:
In [2]: map(sum, zip(m,n,o))
Out[2]: [12, 15, 18]
或numpy.sum
:
In [4]: import numpy as np
In [5]: np.sum([m,n,o],axis=0)
Out[5]: array([12, 15, 18])