no more comparisons
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,int(x2))
> Here I am depending on the lexicographic sorting of tuples.
> Without that there would be real trouble.
I think it needs to be something like:
key = lambda x: (-x, int(x) if x == 0 else -int(x))
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...
More information about the Python-list