elementwise multiplication of 2 lists of numbers
arnodel at gmail.com
Mon Sep 20 18:17:40 CEST 2010
On Sep 20, 3:02 pm, harryos <oswald.ha... at gmail.com> wrote:
> I have 2 lists of numbers,say
> I need to create another list containing
> z=[2*5, 4*9, 3*10, 1*6] ie =[10,36,30,6]
> I did not want to use numpy or any Array types.I tried to implement
> this in python .I tried the following
> for a,b in zip(x,y):
> This gives me the correct result.Still,Is this the correct way?
> Or can this be done in a better way?
> Any pointers most welcome,
In addition to what others have said, you can use the map builtin as
In Python 2.x:
>>> map(int.__mul__, [2, 4, 3, 1], [5, 9, 10, 6])
[10, 36, 30, 6]
If you don't know the list only contain ints use operator.mul.
In Python 3.x you need to wrap it in list() as map returns an
More information about the Python-list