a = [1,2,3]
b = [4,5,6]
Seeking a and b is added to the corresponding elements
1 a = [1,2,3] 2 b = [4,5,6] 3 for i in range(0,3): 4 print(a[i]+b[i])
But only for traversal achieved by adding only the corresponding elements, but the result is not a list:
It needs to be improved:
1 #方法一 2 a = [1,2,3] 3 b = [4,5,6] 4 ls = [] 5 for i in range(0,3): 6 ls.append(a[i]+b[i]) 7 print(ls)
List of Formula
# Method II A = [l, 2,3 ] B = [4,5,6 ] LS = [I + J for (I, J) in ZIP (A, B)] # for the front of a space, not a comma Print (LS)
supplement:
(1) zip () usage:
zip (a, b) is a
Not a + b (this is only two splice list)
also can not:
1 a = [1,2,3] 2 b = [4,5,6] 3 for i in a: 4 for j in b: 5 print(i+j)
Because Layer cycle, after each traversed for the inner loop, only the outer layer out to a for loop. The case of i = 1, j = 4,5,6, rather than just equal to 4; when i = 2, then j = 4,5,6, rather than equal to 5