computing a weighted sum
John Machin
sjmachin at lexicon.net
Wed Mar 16 18:43:06 EST 2005
Fernando Perez wrote:
> andreif at 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 :-)
More information about the Python-list
mailing list