elementwise multiplication of 2 lists of numbers
bruno.42.desthuilliers at websiteburo.invalid
Mon Sep 20 16:28:12 CEST 2010
harryos a écrit :
> 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?
If it gives the expected results then it's at least *a* correct way !-)
> Or can this be done in a better way?
A list comp comes to mind, as well as using itertools.izip if your lists
are a bit on the huge side.
from itertools import izip
z = [a * b for a, b in izip(x, y)]
More information about the Python-list