我现在有:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

我希望有:

[1, 2, 3]
 +  +  +
[4, 5, 6]
|| || ||
[5, 7, 9]

仅仅是两个列表的元素相加。

我当然可以迭代这两个列表,但我不想这样做。

最python化的方式是什么?


当前回答

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]

其他回答

使用map和operator.add:

>>> from operator import add
>>> list( map(add, list1, list2) )
[5, 7, 9]

或者压缩一个列表理解:

>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]

时间比较:

>>> list2 = [4, 5, 6]*10**5
>>> list1 = [1, 2, 3]*10**5
>>> %timeit from operator import add;map(add, list1, list2)
10 loops, best of 3: 44.6 ms per loop
>>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
10 loops, best of 3: 71 ms per loop
>>> %timeit [a + b for a, b in zip(list1, list2)]
10 loops, best of 3: 112 ms per loop
>>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
1 loops, best of 3: 139 ms per loop
>>> %timeit [sum(x) for x in zip(list1, list2)]
1 loops, best of 3: 177 ms per loop

如果您有一个未知数量的列表,并且没有导入任何东西,那么这可能是python式的,稍微有用一些。

只要列表的长度相同,就可以使用下面的函数。

这里*args接受可变数量的列表参数(但每个参数中元素的总和相同)。

在返回的列表中再次使用*来解包每个列表中的元素。

def sum_lists(*args):
    return list(map(sum, zip(*args)))

a = [1,2,3]
b = [1,2,3]  

sum_lists(a,b)

输出:

[2, 4, 6]

或者用3个列表

sum_lists([5,5,5,5,5], [10,10,10,10,10], [4,4,4,4,4])

输出:

[19, 19, 19, 19, 19]

正如其他人所描述的那样,一个快速且节省空间的解决方案是使用numpy (np)及其内置的矢量操作功能:

1. 与Numpy

x = np.array([1,2,3])
y = np.array([2,3,4])
print x+y

2. 与整体功能

2.1λ

list1=[1, 2, 3]
list2=[4, 5, 6]
print map(lambda x,y:x+y, list1, list2)

注意map()支持多个参数。

2.2 zip和列表理解

list1=[1, 2, 3]
list2=[4, 5, 6]
print [x + y for x, y in zip(list1, list2)]

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]

在我看来,使用numpy更简单:

import numpy as np
list1=[1,2,3]
list2=[4,5,6]
np.add(list1,list2)

结果:

有关详细参数信息,请查看这里:numpy.add