computing a weighted sum

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Thu Mar 17 11:26:48 CET 2005


On Thu, 17 Mar 2005 08:11:11 GMT, rumours say that "Raymond Hettinger"
<vze4rx4y at verizon.net> 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]
>> .>> sum(starmap(operator.mul, izip(x,w)))
>> 90.0
>
>Gack!  starmap() is only for situations where the data is already in tuple form.
>If it inputs are already distinct, imap() is the preferred form.
>
>FWIW, the answer was already in the docs (itertools recipes):
>
>    def dotproduct(vec1, vec2):
>        return sum(imap(operator.mul, vec1, vec2))

What, you're some kind of expert on itertools now? :-)

You are of course absolutely correct.  Let my post stand as an example of
itertools misuse.
-- 
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...



More information about the Python-list mailing list