substituting list comprehensions for map()

Robert Kern robert.kern at gmail.com
Tue Nov 3 23:43:45 EST 2009


Steven D'Aprano wrote:
> On Tue, 03 Nov 2009 10:22:28 -0500, J Kenneth King wrote:

>> Adding in the loop construct and name bindings doesn't enhance my
>> understanding of what a dot-product is.  I don't need to see the loop
>> construct at all in this case.  A dot product is simply the
>> multiplication of each element in a vector sequence.
> 
> What you need is to define a function dot-product, and not hijack the 
> name for a local value. Then the function's implementation is irrelevant 
> to you: it could use a list comp, or could use map, it could use a for-
> loop, a while loop, recursion, or black magic:
> 
> scalar = dot_product(vec1, vec2)

Or use the appropriate libraries:

from numpy import dot

scalar = dot(vec1, vec2)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list