max() of a list of tuples

Anton Vredegoor anton at vredegoor.doge.nl
Fri Jan 24 11:43:16 CET 2003


On Thu, 23 Jan 2003 16:54:26 -0800, Erik Max Francis <max at alcyone.com>
wrote:

>The vagueness here is, of course, because reduce works on _any_ function
>that accumulates values over any types of elements, not just numeric
>ones.
>
>Specifying 
>
>	reduce(f, S, i)
>
>is really precisely equivalent to
>
>	reduce(f, [i] + S),

This provides me with a rationale to reuse some code that I did not
post because shorter solutions were already found. It can be used for
lists that are nested deeper than one level.

Regards,
		Anton.

from operator import getitem

def maxat(L,x):
    def getat(indices):
        return reduce(getitem,indices,L)
    def imax(i,j):
        if getat([i]+x)<getat([j]+x): return j
        return i
    return L[reduce(imax,range(len(L)))]
    
def test():
    L = [(1,3,5), (8,16,2), (2,56,4)]
    print maxat(L,[2])

if __name__=='__main__':
    test()







More information about the Python-list mailing list