440,235 Members | 1,008 Online
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

# computing a weighted sum

 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[n-1]*w[n-1]. 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 Jul 18 '05 #1
10 Replies

 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[n-1]*w[n-1]. 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 Jul 18 '05 #2

 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[n-1]*w[n-1]. 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 Jul 18 '05 #3

 P: n/a Thanks Will, the 2.4 expression looks really nice. Jul 18 '05 #4

 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 wantto compute x[0]*w[0] + .. + x[n-1]*w[n-1].Is there some elegant expression (perhaps using lambda) to have it donein 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... Jul 18 '05 #5

 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 Jul 18 '05 #6

 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 Jul 18 '05 #7

 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[n-1]*w[n-1]. 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 Jul 18 '05 #8

 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[n-1]*w[n-1]. 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 :-) Jul 18 '05 #9

 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 Jul 18 '05 #10

 P: n/a On Thu, 17 Mar 2005 08:11:11 GMT, rumours say that "Raymond Hettinger" 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.0Gack! 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... Jul 18 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.