Python sums the corresponding elements of two list lists

Python sums the corresponding elements of two list lists

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

Sum list a and list b by subscript results in [5,7,9]

1:
sum = [
    a[0] + b[0],
    a[1] + b[1],
    a[2] + b[2]
]

2:
sum = [a[i]+b[i] for i in range(min(len(a),len(b)))] 

3:

sum = list(map(lambda x :x[0]+x[1] ,zip(a,b)))

4:
import numpy as np
c = np.array(a) + np.array(b)

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example