Normalizing A Vector

Thomas Jollans thomas at jollans.com
Sat Jul 31 06:44:15 EDT 2010


On 07/31/2010 12:15 PM, Lawrence D'Oliveiro wrote:
>reduce(lambda a, b : a + b, (y * y for y in V), 0))
> 

This is a lot more verbose than it has to be, and probably slower too.

firstly:

(lambda a,b: a+b) is equivalent to operator.add.

==>
reduce(operator.add, (y*y for y in V), 0)

However - reduce with operator.add ? we have a special name for this:

==>
sum(y*y for y in V)



More information about the Python-list mailing list