elementwise multiplication of 2 lists of numbers

H

harryos

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
 
G

Gary Herron

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
 
B

Bruno Desthuilliers

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)]
 
G

Giacomo Boffi

harryos said:
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)
 
H

harryos

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

harry
 
A

Arnaud Delobelle

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
 
P

Peter Otten

harryos said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top