P: n/a

Suppose I have a list of n floats x and a list of n floats w and I want
to compute x[0]*w[0] + .. + x[n1]*w[n1].
Is there some elegant expression (perhaps using lambda) to have it done
in one statement ? As in :
y = lambda x,w : ...
I ask because the way I am doing it now :
y = 0
for i in range(0,n): y += x[i]*w[i]
doesn't seem very pythonic :)
Thanks,
Andrei  
Share this Question
P: n/a
 an*****@mail.dntis.ro wrote: Suppose I have a list of n floats x and a list of n floats w and I want to compute x[0]*w[0] + .. + x[n1]*w[n1].
Is there some elegant expression (perhaps using lambda) to have it done in one statement ? As in : y = lambda x,w : ...
I ask because the way I am doing it now : y = 0 for i in range(0,n): y += x[i]*w[i]
doesn't seem very pythonic :)
I'll take a stab at that!
In Python 2.3
sum( [ _x * _w for _x, _w in zip( x, w ) ] )
or in 2.4
sum( _x * _w for _x, _w in zip( x, w ) )
You may want to use itertools.izip in place of zip if the lists are large.
Will McGugan  
P: n/a

wrote: Suppose I have a list of n floats x and a list of n floats w and I want to compute x[0]*w[0] + .. + x[n1]*w[n1].
Is there some elegant expression (perhaps using lambda) to have it done in one statement ? As in : y = lambda x,w : ... x = [1, 2, 3] w = [4, 5, 6] sum(a*b for (a,b) in zip(x, w))
32  
P: n/a

Thanks Will, the 2.4 expression looks really nice.  
P: n/a

On 16 Mar 2005 06:49:09 0800, rumours say that an*****@mail.dntis.ro might have
written: Suppose I have a list of n floats x and a list of n floats w and I want to compute x[0]*w[0] + .. + x[n1]*w[n1].
Is there some elegant expression (perhaps using lambda) to have it done in one statement ? As in : y = lambda x,w : ...
I ask because the way I am doing it now : y = 0 for i in range(0,n): y += x[i]*w[i]
doesn't seem very pythonic :)
Your method seems to be the one closest to what's generally considered as
pythonic.
Anyway, a functional equivalent:
..>> from itertools import starmap, izip
..>> import operator
..>> x= [1,2,3,4]
..>> w=[3.0, 6.0, 9.0, 12.0]
..>> sum(starmap(operator.mul, izip(x,w)))
90.0
..>>

TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...  
P: n/a

Will McGugan wrote: In Python 2.3
sum( [ _x * _w for _x, _w in zip( x, w ) ] )
or in 2.4
sum( _x * _w for _x, _w in zip( x, w ) )
Any reason for the leading underscores? If you're trying to avoid
polluting your namespace, you should note that generator expressions
don't leak their loop variables, so you can write the second as:
sum(x*w for x, w in zip(x, w))
without any worries of overwriting x and w. (Of course I would probably
name them something different anyway, e.g. x_item and w_item...)
STeVe  
P: n/a

Even if language permits
sum(x*w for x, w in zip(x, w))
would seem confusing for anyone watching the code
Maybe
sum(xi*wi for xi, wi in zip(x, w))
would be more appropiate
Andrei  
P: n/a
 an*****@mail.dntis.ro wrote: Suppose I have a list of n floats x and a list of n floats w and I want to compute x[0]*w[0] + .. + x[n1]*w[n1].
Is there some elegant expression (perhaps using lambda) to have it done in one statement ? As in : y = lambda x,w : ...
I ask because the way I am doing it now : y = 0 for i in range(0,n): y += x[i]*w[i]
doesn't seem very pythonic :)
Thanks, Andrei
import Numeric
print Numeric.dot(x,w)
Best,
f  
P: n/a

Fernando Perez wrote: an*****@mail.dntis.ro wrote:
Suppose I have a list of n floats x and a list of n floats w and I
want to compute x[0]*w[0] + .. + x[n1]*w[n1].
Is there some elegant expression (perhaps using lambda) to have it
done in one statement ? As in : y = lambda x,w : ...
I ask because the way I am doing it now : y = 0 for i in range(0,n): y += x[i]*w[i]
doesn't seem very pythonic :)
Thanks, Andrei
import Numeric print Numeric.dot(x,w)
Indeed. Horses for courses. Anyway, people who reinvent the wheel often
fall to arguing among themselves whose polygon is the best
approximation to a circle, and forget to reinvent the axle. Wouldn't
happen in this newsgroup, of course :)  
P: n/a

[Christos TZOTZIOY Georgiou] Anyway, a functional equivalent:
.>> from itertools import starmap, izip .>> import operator .>> x= [1,2,3,4] .>> w=[3.0, 6.0, 9.0, 12.0] .>> sum(starmap(operator.mul, izip(x,w))) 90.0
Gack! starmap() is only for situations where the data is already in tuple form.
If it inputs are already distinct, imap() is the preferred form.
FWIW, the answer was already in the docs (itertools recipes):
def dotproduct(vec1, vec2):
return sum(imap(operator.mul, vec1, vec2))
Raymond Hettinger  
P: n/a

On Thu, 17 Mar 2005 08:11:11 GMT, rumours say that "Raymond Hettinger"
<vz******@verizon.net> might have written: [Christos TZOTZIOY Georgiou] Anyway, a functional equivalent:
.>> from itertools import starmap, izip .>> import operator .>> x= [1,2,3,4] .>> w=[3.0, 6.0, 9.0, 12.0] .>> sum(starmap(operator.mul, izip(x,w))) 90.0
Gack! starmap() is only for situations where the data is already in tuple form. If it inputs are already distinct, imap() is the preferred form.
FWIW, the answer was already in the docs (itertools recipes):
def dotproduct(vec1, vec2): return sum(imap(operator.mul, vec1, vec2))
What, you're some kind of expert on itertools now? :)
You are of course absolutely correct. Let my post stand as an example of
itertools misuse.

TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 4533
 replies: 10
 date asked: Jul 18 '05
