computing a weighted sum
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: computing a weighted sum
Thanks Will, the 2.4 expression looks really nice. -- http://mail.python.org/mailman/listinfo/python-list
Re: computing a weighted sum
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
