max() of a list of tuples

Erik Max Francis max at alcyone.com
Thu Jan 23 01:30:44 CET 2003


Peter Abel wrote:

> Believe me, I'm not at all happy with (-1.e16,-1.e16,-1.e16).
> It's only because nothing else came into my head.
> I tried ('','','') and it worked too ?? But
> as I remarked later, only in this special case.

It's implementation defined; all strings could test greater than all
numerics in a particular Python implementation, for instance.

> So the solution is to start with the first element of l.
> And the the tuples can be what they want: integer, floats etc.
> 
> reduce(lambda y,x:y[2]>x[2] and y or x,l,l[0])
> 
> I think it's the best initial value one can choose.

It's not clear to me why you think an initial value here is necessary in
the first place.  Since you're only doing the equivalent a reduce on a
max function, the only case you'd need to specify that third argument is
if there were nothing to reduce (i.e., the list were empty).  But you
can simply handle that case separately before you call reduce, so it's a
non-issue.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ I'm rolling like thunder / I'm feeling my power
\__/ Chante Moore
    CatCam / http://www.catcam.com/
 What do your pets do all day while you're at work?  Find out.




More information about the Python-list mailing list