reduce()--what is it good for? (was: Re: reduce() anomaly?)

David Eppstein eppstein at ics.uci.edu
Fri Nov 7 00:57:33 EST 2003


In article <mailman.507.1068171071.702.python-list at python.org>,
 Bob Gailer <bgailer at alum.rpi.edu> wrote:

> To expand your reduce horizons in Python, consider reduce as a way to 
> examine the relationship between successive pairs of a sequence. For 
> example one might wants the difference between successive elements: given 
> seq = [1,3,4,7] we'd like a differences == [2,1,3].
> 
> differences = []
> def neighborDifference(left, right):
>    differences.append(right - left)
>    return right
> reduce(neighborDifference, seq)

seq=[1,3,4,7]
[y-x for x,y in zip(seq,seq[1:])]

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list