no more comparisons

Alan Isaac aisaac at american.edu
Thu Mar 13 20:38:40 EDT 2008


Mark Dickinson wrote:

> 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.  Or even worse, where the ordering of the 

> second item depends on the value of the first item in the 

> tuple. 



This is like some examples where I had used cmp,

but if I understand correctly I think it is not a problem.



> For example, suppose that (for whatever contrived reason)

> you're representing integers in (sign, magnitude) format 

> by tuples (s, i), where s = 0 or 1 (0 for positive, 1 for 

> negative) and i is a string representing the absolute 

> value of the integer.  So 



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.



Cheers,

Alan Isaac





More information about the Python-list mailing list