List comprehension同时迭代两个变量

时间:2016-07-08 14:47:42

标签: python python-2.7 python-3.x

使用列表推导是否有可能同时迭代两个变量同时增加两个循环的位置。见下面的例子:

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](来自b的+ n'元素中的第n个元素)

谢谢。

1 个答案:

答案 0 :(得分:6)

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]