问题
Is it possible that with the use of list comprehension to iterate through two variables at the same time increasing the loop position in both at the same time. See example below:
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.
expected output is c = [7, 9, 11, 13, 15]
(n'th element from a + n'th element from b)
Thank you.
回答1:
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]
来源:https://stackoverflow.com/questions/38269538/list-comprehension-iterate-two-variables-at-the-same-time