我现在有:

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

我希望有:

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

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

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

最python化的方式是什么?


当前回答

如果您需要处理不同大小的列表,不用担心!很棒的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。

参见相关回答,并对另一个问题进行评论。

其他回答

虽然,实际的问题并不想遍历列表来生成结果,但是所提出的所有解决方案实际上都是这样做的!

要刷新:如果不查看所有向量元素,就不能将两个向量相加。因此,大多数解的算法复杂度都是大o (n)。其中n是向量的维数。

因此,从算法的角度来看,使用for循环迭代生成结果列表是合乎逻辑的,也是python化的。但是,除此之外,该方法没有调用或导入任何附加库的开销。

# Assumption: The lists are of equal length.
resultList = [list1[i] + list2[i] for i in range(len(list1))]

这里显示/讨论的时间取决于系统和实现,不能作为衡量操作效率的可靠措施。在任何情况下,向量加法运算的大O复杂度是线性的,即O(n)。

使用map与lambda函数:

>>> map(lambda x, y: x + y, list1, list2)
[5, 7, 9]

其他人给出了如何在纯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左右开始时更快。

有几种方法

使用Numpy

import numpy as np
x = np.array([2,3,3])
y = np.array([1,2,6])

print(type(x)) # <class 'numpy.ndarray'>
print(type(y)) # <class 'numpy.ndarray'>

print(x+y) # [3 5 9]
print(type(x+y)) # <class 'numpy.ndarray'>

在上面的代码中,你可以看到输入和输出都是NumPy数组格式。

import numpy as np
list1=[4,2,2,5]
list2=[2,1,6,7]

print(type(list1)) # <class 'list'>
print(type(list2)) # <class 'list'>

print(np.add(list1,list2)) # [ 6  3  8 12]
print(type(np.add(list1,list2))) # <class 'numpy.ndarray'>

这里输入和输出是不同的格式。

使用Numpy添加

import numpy as np
list1=[3, 1, 4]
list2=[0, 9, 7]

print(type(list1)) # <class 'list'>
print(type(list2)) # <class 'list'>

print(np.add(list1, list2).tolist()) # [3, 10, 11]
print(type(np.add(list1, list2).tolist())) # <class 'list'>

在这个例子中,我们显式地使用to_list()将NumPy数组转换为列表类型

使用Map和Lambda

list1=[1, 3, 3]
list2=[3, 6, 8]

print(map(lambda x,y:x+y, list1, list2)) # <map object at 0x7fea235260a0>
print(list(map(lambda x,y:x+y, list1, list2))) # [4, 9, 11]

使用zip和列表理解

list1=[3, 1, 3]
list2=[1, 1, 3]

print(type(list1)) # <class 'list'>
print(type(list2)) # <class 'list'>

print(x + y for x, y in zip(list1, list2)) # <generator object <genexpr> at 0x7f755307b6d0>
print(list(x + y for x, y in zip(list1, list2))) # [4, 2, 6]
print(type([x + y for x, y in zip(list1, list2)])) # <class 'list'>

print(sum(x) for x in zip(list1, list2)) # <generator object <genexpr> at 0x7f4c623e76d0>
print(list(sum(x) for x in zip(list1, list2))) # [4, 2, 6]
print(type([sum(x) for x in zip(list1, list2)])) # <class 'list'>

使用Map和operator.add

from operator import add
list1=[3, 1, 3]
list2=[1, 1, 3]
print(list(map(add, list1, list2))) # [4, 2, 6]
[a + b for a, b in zip(list1, list2)]