no more comparisons

Alan Isaac aisaac at american.edu
Fri Mar 14 00:28:41 EDT 2008


Dan Bishop wrote:

> Even assuming you meant (-x[0], int(x[1])), that sorts negative 

> numbers in the wrong order.  You want 

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





Sorry, was speed typing (very badly) and the question 

actually had two different problem descriptions.

As you suggest, what I meant was::



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



which was meant to address:



    "Sorting tuples, where the second item in the tuple 

    should have the opposite ordering to the first is going 

    to be a bit of a pain."



But you are quite right, the problem was then specified to 

numerical order, for which I like your solution.  Or even::



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





The key point being, if you will, that this use case does 

not support the idea that relying on ``key`` will be so bad.  

So, what is a case that is really uncomfortable?



Thanks,

Alan Isaac





More information about the Python-list mailing list