我现在有:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
我希望有:
[1, 2, 3]
+ + +
[4, 5, 6]
|| || ||
[5, 7, 9]
仅仅是两个列表的元素相加。
我当然可以迭代这两个列表,但我不想这样做。
最python化的方式是什么?
我现在有:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
我希望有:
[1, 2, 3]
+ + +
[4, 5, 6]
|| || ||
[5, 7, 9]
仅仅是两个列表的元素相加。
我当然可以迭代这两个列表,但我不想这样做。
最python化的方式是什么?
当前回答
这将适用于2个或更多的列表;遍历列表的列表,但使用numpy加法处理每个列表的元素
import numpy as np
list1=[1, 2, 3]
list2=[4, 5, 6]
lists = [list1, list2]
list_sum = np.zeros(len(list1))
for i in lists:
list_sum += i
list_sum = list_sum.tolist()
[5.0, 7.0, 9.0]
其他回答
使用map与lambda函数:
>>> map(lambda x, y: x + y, list1, list2)
[5, 7, 9]
也许“最python化的方式”应该包括处理list1和list2大小不同的情况。运用其中的一些方法,你会不动声色地得到答案。numpy方法会让您知道,很可能使用ValueError。
例子:
import numpy as np
>>> list1 = [ 1, 2 ]
>>> list2 = [ 1, 2, 3]
>>> list3 = [ 1 ]
>>> [a + b for a, b in zip(list1, list2)]
[2, 4]
>>> [a + b for a, b in zip(list1, list3)]
[2]
>>> a = np.array (list1)
>>> b = np.array (list2)
>>> a+b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2) (3)
如果这是你问题中的函数你想要什么结果?
zip函数在这里很有用,它与列表推导式v1, v2一起使用。 如果你有一个列表的列表(而不是两个列表),你可以使用v3。 对于具有不同长度的列表(例如:通过在第一个/第二个列表的末尾添加1),那么您可以尝试类似这样的操作(使用zip_longest) - v4
first = [1, 2, 3, 1]
second = [4, 5, 6]
output: [5, 7, 9, 1]
如果拥有相同长度的未知数量的列表,则可以使用函数v5。 v6 - operator模块导出一组与Python的内在操作符对应的高效函数。例如,operator。Add (x, y)等价于表达式x+y。 v7 -假设第一个列表和第二个列表具有相同的长度,您不需要zip或其他任何东西。
################
first = [1, 2, 3]
second = [4, 5, 6]
####### v1 ########
third1 = [sum(i) for i in zip(first,second)]
####### v2 ########
third2 = [x + y for x, y in zip(first, second)]
####### v3 ########
lists_of_lists = [[1, 2, 3], [4, 5, 6]]
third3 = [sum(x) for x in zip(*lists_of_lists)]
####### v4 ########
from itertools import zip_longest
third4 = list(map(sum, zip_longest(first, second, fillvalue=0)))
####### v5 ########
def sum_lists(*args):
return list(map(sum, zip(*args)))
third5 = sum_lists(first, second)
####### v6 ########
import operator
third6 = list(map(operator.add, first,second))
####### v7 ########
third7 =[first[i]+second[i] for i in range(len(first))]
####### v(i) ########
print(third1) # [5, 7, 9]
print(third2) # [5, 7, 9]
print(third3) # [5, 7, 9]
print(third4) # [5, 7, 9]
print(third5) # [5, 7, 9]
print(third6) # [5, 7, 9]
print(third7) # [5, 7, 9]
如果您需要处理不同大小的列表,不用担心!很棒的itertools模块已经介绍过了:
>>> from itertools import zip_longest
>>> list1 = [1,2,1]
>>> list2 = [2,1,2,3]
>>> [sum(x) for x in zip_longest(list1, list2, fillvalue=0)]
[3, 3, 3, 3]
>>>
在Python 2中,zip_longest被称为izip_longest。
参见相关回答,并对另一个问题进行评论。
其他人给出了如何在纯python中做到这一点的示例。如果你想对包含100.000个元素的数组执行此操作,你应该使用numpy:
In [1]: import numpy as np
In [2]: vector1 = np.array([1, 2, 3])
In [3]: vector2 = np.array([4, 5, 6])
现在,按元素进行添加非常简单
In [4]: sum_vector = vector1 + vector2
In [5]: print sum_vector
[5 7 9]
就像Matlab一样。
与Ashwini的最快版本进行比较的时间:
In [16]: from operator import add
In [17]: n = 10**5
In [18]: vector2 = np.tile([4,5,6], n)
In [19]: vector1 = np.tile([1,2,3], n)
In [20]: list1 = [1,2,3]*n
In [21]: list2 = [4,5,6]*n
In [22]: timeit map(add, list1, list2)
10 loops, best of 3: 26.9 ms per loop
In [23]: timeit vector1 + vector2
1000 loops, best of 3: 1.06 ms per loop
所以这要快25倍!但是要用适合你的情况。对于一个简单的程序,您可能不想安装numpy,因此使用标准的python(我发现Henry的版本是最python的版本)。如果你喜欢严肃的数字运算,让numpy来做繁重的工作。对于速度狂人来说:似乎numpy解决方案在n = 8左右开始时更快。