substituting list comprehensions for map()
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Wed Nov 4 00:35:30 EST 2009
On Tue, 03 Nov 2009 22:43:45 -0600, Robert Kern wrote:
> 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)
Why would I want to use an already existing library that is fast, well-
written and well-supported, when I can toss together a nasty kludge
myself?
--
Steven
More information about the Python-list
mailing list