Input: two lists (list1, list2) of equal length
Output: one list (result) of the same length, such that:
result[i] = list1[i] + list2[i]
有什么简洁的方法可以做到这一点吗?或者这是最好的:
# Python 3
assert len(list1) == len(list2)
result = [list1[i] + list2[i] for i in range(len(list1))]
最佳答案
您可以使用内置的 zip函数,或者您也可以使用 do it 将两个列表映射到 add 运算符上。像这样:
from operator import add
map(add, list1,list2)
关于python:逐点列表总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5788470/