2

So, when sorting a list in python, the sorted function can take a cmp keyword to override the __cmp__ function of the objects we are sorting.

I would expect max to have a similar keyword, but it doesn't. People know why?

And in any case, anyone know the most pythonic way to get around this? I don't want to override __cmp__ for the classes themselves, and other options I can think of like sorted(L,cmp=compare)[0] seem ugly. What would be a nice way to do this?

The actual example is given as L=[a1,a2,...,an] where each ak is itself a list of integers we want the maximum where ai<aj is in the lexicographical sense.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
SamBob
  • 849
  • 6
  • 17

1 Answers1

4

Don't use cmp. It has been removed from Python3 in favor of key.

Since Python compares strings lexicographically, you could use key = str to find the "maximum" integer:

In [2]: max([10,9], key = str)
Out[2]: 9
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 3
    Ah, now that you've mentioned it, I see? So I can use `functools.cmp_to_key` with my comparison function for `max` and `sorted` – SamBob Aug 07 '12 at 11:00