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]
>
[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
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 =
[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
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
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
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 ?
Thanks Will, the 2.4 expression looks really nice.
--
http://mail.python.org/mailman/listinfo/python-list
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]
[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
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
11 matches
Mail list logo