我如何在python中添加任意数量的列表

时间:2014-02-04 04:26:45

标签: python list

我正在尝试像我在这里找到的功能一样添加列表,但问题是我无法弄清楚如何添加超过特定数量的列表。我希望它添加任意数量的列表,所以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)

1 个答案:

答案 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])