no more comparisons

Mark Dickinson dickinsm at gmail.com
Fri Mar 14 02:50:18 CET 2008


On Mar 13, 8:38 pm, Alan Isaac <ais... at american.edu> wrote:
> Does this do it? ::
>
>     key= lambda x: (-x[1],int(x2))
>
> Here I am depending on the lexicographic sorting of tuples.
> Without that there would be real trouble.

Close. :-)

I think it needs to be something like:

key = lambda x: (-x[0], int(x[1]) if x[0] == 0 else -int(x[1]))

Relying on lexicographic sorting of tuples seems okay to me.

What bugs me more about this example is that one has to rely
on the existence of a way to negate the usual ordering.  This
is fine for numbers (where you can use the unary - operator)
or for numeric strings (where you can convert to int and then
use -), but it's not too much of a stretch to imagine cases
where neither of those options is available (e.g. non-numeric
strings), and where one ends up moving further and further
from readable code and into the realm of arcane trickery...

Mark



More information about the Python-list mailing list