xxjjnn xxjjnn - 1 year ago 198
Python Question

python element-wise multiplication of two lists

I want to perform an element wise multiplication, to multiply two lists together by value:

a = [1,2,3,4]
b = [2,3,4,5]
a.*b = [2,6,12,20]

in Python. (That works in Matlab)

A List Comprehensions would give 16 list entries, for every combination x*y of x from a and y from b. Unsure of how to map this.

If anyone is interested why, I have a dataset, and want to multiply it by Numpy.linspace(1.0,0.5,num=len(dataset)) =)

Answer Source

Use a list comprehension mixed with zip():.

[a*b for a,b in zip(lista,listb)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download