usage of reduce

Carel Fellinger cfelling at iae.nl
Tue May 8 17:30:29 CEST 2001


...
> try:
> reduce(lambda x,y: x+y, map(lambda tup: tup[1], z))

or specify an initial value simplifying the lambda:

>>> reduce(lambda x,y: x+y[1], z, 0)

This is usefull in other cases were the outcome of the reduce differs
in type from the values on which it's to operate. like:

>>> reduce(lambda x,y: x+[2*y], [1,2,3], [])
[2, 4, 6]

>>> reduce(lambda x,y: x + int(y), [1.1, 2.2, 3.3], 0)
6



-- 
groetjes, carel



More information about the Python-list mailing list