Python: Dot product of each vector in two lists of vectors -


say have 2 lists containing vector:

a = [(1,1,1), (0,1,1)] b = [(1,0,1), (1,0,0)] 

i hope perform dot product between each vector elementwise output

c = [2, 0] 

how can in python?

in pure python, try nested list/generator comprehension:

>>> [sum(ai * bi ai, bi in zip(a, b)) ...  a, b in zip(a, b)] [2, 0] 

or numpy, can element-wise product of 2-dimensional arrays, followed sum along each row:

>>> import numpy np >>> np.multiply(a, b).sum(1) array([2, 0]) 

note numpy solution work if vectors same length – each list of vectors implicitly converted two-dimensional matrix.