computing a weighted sum

Duncan Booth duncan.booth at invalid.invalid
Wed Mar 16 09:58:53 EST 2005


 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



More information about the Python-list mailing list