Re: computing a weighted sum

2005-03-17 Thread TZOTZIOY
On Thu, 17 Mar 2005 08:11:11 GMT, rumours say that "Raymond Hettinger" <[EMAIL PROTECTED]> 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] >

Re: computing a weighted sum

2005-03-17 Thread Raymond Hettinger
[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

Re: computing a weighted sum

2005-03-16 Thread John Machin
Fernando Perez wrote: > [EMAIL PROTECTED] 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 =

Re: computing a weighted sum

2005-03-16 Thread Fernando Perez
[EMAIL PROTECTED] 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 t

Re: computing a weighted sum

2005-03-16 Thread andreif
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: computing a weighted sum

2005-03-16 Thread Steven Bethard
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

Re: computing a weighted sum

2005-03-16 Thread TZOTZIOY
On 16 Mar 2005 06:49:09 -0800, rumours say that [EMAIL PROTECTED] 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[n-1]*w[n-1]. > >Is there some elegant expression (perhaps using lambda) to have it done >in one statement ?

Re: computing a weighted sum

2005-03-16 Thread andreif
Thanks Will, the 2.4 expression looks really nice. -- http://mail.python.org/mailman/listinfo/python-list

Re: computing a weighted sum

2005-03-16 Thread Duncan Booth
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]

Re: computing a weighted sum

2005-03-16 Thread Will McGugan
[EMAIL PROTECTED] 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 i

computing a weighted sum

2005-03-16 Thread andreif
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