关于python:List comprehension同时迭代两个变量

List comprehension iterate two variables at the same time

本问题已经有最佳答案,请猛点这里访问。

是否可能使用列表理解同时迭代两个变量,同时增加两个变量中的循环位置?见下例:

1
2
3
4
5
a = [1,2,3,4,5]

b = [6,7,8,9,10]

c = [i+j for i in a for j in b] # This works but the output is not what it would be expected.

预期输出为c = [7, 9, 11, 13, 15](第n个元素来自a+n个元素来自b)

谢谢您。


1
2
3
4
5
6
7
8
a = [1,2,3,4,5]
b = [6,7,8,9,10]

c = map(sum, zip(a, b))
print c

#Output
[7, 9, 11, 13, 15]