Dot product?

Charles Boncelet boncelet at udel.edu
Mon Dec 13 04:52:36 EST 1999


Greg Ewing wrote:
> 
> "Magnus L. Hetland" wrote:
...
> 
> >
> > And is
> >
> >   for x,y in list1, list2:
> >
> > ruled out because of anything except aesthetic preference?
> 
> Yes. You're already allowed a comma-separated list of
> target variables, in which case unpacking occurs.
> 
How about the the mxTools solution:

sum = 0.0
for x,y in tuples(list1, list2):
    sum = sum + x*y

(On my soapbox): I like "tuples(list1, list2)" much better than 
"map(None, list1, list2)" because the former seems much clearer as to 
the intent of what the expression is supposed to do.

AFAIK, both "tuples" and "map" solutions create an intermediate object.
However there seems to be no reason why the internals of Python couldn't 
be changed so that these could be computed "on the fly" as needed.

Of course, if David (who started this thing) is really interested in
computing dot products, he should look at Numeric.
-- 
Charles Boncelet <boncelet at udel.edu>
University of Delaware
Newark DE 19716 USA
http://www.eecis.udel.edu/~boncelet/



More information about the Python-list mailing list