Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   elementwise multiplication of 2 lists of numbers (http://www.velocityreviews.com/forums/t733707-elementwise-multiplication-of-2-lists-of-numbers.html)

 harryos 09-20-2010 02:02 PM

elementwise multiplication of 2 lists of numbers

hi
I have 2 lists of numbers,say
x=[2,4,3,1]
y=[5,9,10,6]
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

z=[]
for a,b in zip(x,y):
z.append(a*b)
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,
harry

 Gary Herron 09-20-2010 02:26 PM

Re: elementwise multiplication of 2 lists of numbers

On 09/20/2010 07:02 AM, harryos wrote:
> hi
> I have 2 lists of numbers,say
> x=[2,4,3,1]
> y=[5,9,10,6]
> 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
>
> z=[]
> for a,b in zip(x,y):
> z.append(a*b)
> 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,
> harry
>

List comprehension might be considered better by some, but that's a
subjective judgment. (One with which I agree.) List comprehension may
also be faster, but you'd have to test to know for sure.

>>> x=[2,4,3,1]
>>> y=[5,9,10,6]
>>> z = [a*b for a,b in zip(x,y)]
>>> print z

[10, 36, 30, 6]

Gary Herron

 Bruno Desthuilliers 09-20-2010 02:28 PM

Re: elementwise multiplication of 2 lists of numbers

harryos a écrit :
> hi
> I have 2 lists of numbers,say
> x=[2,4,3,1]
> y=[5,9,10,6]
> 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
>
> z=[]
> for a,b in zip(x,y):
> z.append(a*b)
> 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)]

 Giacomo Boffi 09-20-2010 02:39 PM

Re: elementwise multiplication of 2 lists of numbers

harryos <oswald.harry@gmail.com> writes:

> hi
> I have 2 lists of numbers,say
> x=[2,4,3,1]
> y=[5,9,10,6]
> 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
>
> z=[]
> for a,b in zip(x,y):
> z.append(a*b)
> This gives me the correct result.Still,Is this the correct way?
> Or can this be done in a better way?

what you've done is correct, rather than in better ways this can be
done in different ways

first, there is list comprehension
>>> [x*y for x,y in zip([2,4,3,1],[5,9,10,6])]

[10, 36, 30, 6]
>>>

if you feel that "zip" looks like an artifact, python has some
functional bit
>>> map(lambda x,y: x*y, [2,4,3,1],[5,9,10,6])

[10, 36, 30, 6]
>>>

if you feel that "lambda" looks like an artifact,
>>> from operator import mul
>>> map(mul, [2,4,3,1],[5,9,10,6])

[10, 36, 30, 6]
>>>

hth,
--
> In tutti noi c'è un lato interista

Lato perlopiù nascosto dalle mutande.
--- Basil Fawlty, a reti unificate (IFQ+ISC)

 harryos 09-20-2010 02:40 PM

Re: elementwise multiplication of 2 lists of numbers

On Sep 20, 7:28*pm, Bruno wrote:
>> A list comp comes to mind, as well as using itertools.izip

thanks Bruno,thanks Gary..
Should have thought of list comprehension..
Thanks for the pointer about izip

harry

 Arnaud Delobelle 09-20-2010 04:17 PM

Re: elementwise multiplication of 2 lists of numbers

On Sep 20, 3:02*pm, harryos <oswald.ha...@gmail.com> wrote:
> hi
> I have 2 lists of numbers,say
> x=[2,4,3,1]
> y=[5,9,10,6]
> 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
>
> z=[]
> for a,b in zip(x,y):
> * * * * z.append(a*b)
> 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,
> harry

In addition to what others have said, you can use the map builtin as
follows:
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
iterator.

HTH

--
Arnaud

 Peter Otten 09-20-2010 04:33 PM

Re: elementwise multiplication of 2 lists of numbers

harryos wrote:

> I have 2 lists of numbers,say
> x=[2,4,3,1]
> y=[5,9,10,6]
> 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
>
> z=[]
> for a,b in zip(x,y):
> z.append(a*b)
> 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,

Finally, if you have a lot of data that you want to process efficiently
consider using numpy arrays instead of lists:

>>> import numpy
>>> x = numpy.array([2,4,3,1])
>>> y = numpy.array([5,9,10,6])
>>> x*y

array([10, 36, 30, 6])

Peter

 All times are GMT. The time now is 12:58 PM.