Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to sort this without 'cmp=' in python 3?

2,630 views
Skip to first unread message

38016...@gmail.com

unread,
Oct 14, 2016, 7:35:08 PM10/14/16
to
nums=['3','30','34','32','9','5']
I need to sort the list in order to get the largest number string: '953433230'

nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)

But how to do this in python 3?

Thank you

Robin Koch

unread,
Oct 14, 2016, 7:49:33 PM10/14/16
to
https://docs.python.org/3/library/functools.html#functools.cmp_to_key

| Transform an old-style comparison function to a key function.

--
Robin Koch



sohca...@gmail.com

unread,
Oct 14, 2016, 7:53:48 PM10/14/16
to
You don't need a lambda in this case.

Sort the strings in reverse order:
nums.sort(reverse=True)

Create a string:
biggestNum = ''.join(nums)

Or in a single line, which doesn't change the original value of nums:
biggestNum = ''.join(sorted(nums, reverse=True))

Ned Batchelder

unread,
Oct 14, 2016, 7:58:22 PM10/14/16
to
cmp_to_key is a neat magic trick of a function, because it's seemingly
doing the impossible: how can a two-argument function be turned into
a one-argument function?

Studying the code turns up a few clever Python tricks.

--Ned.

Ben Finney

unread,
Oct 14, 2016, 7:59:58 PM10/14/16
to
38016...@gmail.com writes:

> nums=['3','30','34','32','9','5']
> I need to sort the list in order to get the largest number string: '953433230'
> nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)

For demonstration, I'll re-write this such that the names and output
make more sense::

$ python2
Python 2.7.12+ (default, Sep 1 2016, 20:27:38)
[GCC 6.2.0 20160927] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> digits = ['3','30','34','32','9','5']
>>> sorted(
... digits,
... cmp=(lambda a, b: cmp(a+b, b+a)),
... reverse=True)
['9', '5', '34', '3', '32', '30']

> But how to do this in python 3?

The Python 3 sorting functions take a ‘key’ parameter:

`key` specifies a function of one argument that is used to extract a
comparison key from each list element: `key=str.lower`. The default
value is `None` (compare the elements directly).

<URL:https://docs.python.org/3/library/functions.html#sorted>

The `functools.cmp_to_key` helper is designed to help you transition to
that style:

functools.cmp_to_key(func)

Transform an old-style comparison function to a key function. Used
with tools that accept key functions (such as sorted(), min(),
max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby()).
This function is primarily used as a transition tool for programs
being converted from Python 2 which supported the use of comparison
functions.

A comparison function is any callable that accept two arguments,
compares them, and returns a negative number for less-than, zero for
equality, or a positive number for greater-than. A key function is a
callable that accepts one argument and returns another value to be
used as the sort key.

<URL:https://docs.python.org/3/library/functools.html#functools.cmp_to_key>

This works in the latest Python 2 and Python 3.

>>> import functools
>>> digits = ['3','30','34','32','9','5']
>>> sorted(
... digits,
... key=functools.cmp_to_key(lambda a, b: cmp(a+b, b+a)),
... reverse=True)
['9', '5', '34', '3', '32', '30']

The trick is done by creating a key function that takes an item for
comparison, and returns a custom object, which knows how to compare
itself as specified by your comparison function.

>>> key_func = functools.cmp_to_key(lambda a, b: cmp(a+b, b+a))
>>> key_func("32")
<functools.K object at 0x7f6781ce0980>
>>> key_func("32") < key_func("5")
True

See the Sorting HOWTO <URL:https://docs.python.org/3/howto/sorting.html>
for this and other tricks.

--
\ “I used to be an airline pilot. I got fired because I kept |
`\ locking the keys in the plane. They caught me on an 80 foot |
_o__) stepladder with a coathanger.” —Steven Wright |
Ben Finney

bream...@gmail.com

unread,
Oct 15, 2016, 12:04:59 AM10/15/16
to
No, you've made exactly the same mistake that I did :(

>>> nums=['3','30','34','32','9','5']
>>> wants='953433230'
>>> nums.sort(reverse=True)
>>> result = ''.join(nums)
>>> result == wants
False
>>> result
'953432303'

Peter Otten

unread,
Oct 15, 2016, 3:27:42 AM10/15/16
to
While cmp_to_key is neat doing it by hand should also be instructive.
Essentially you move the comparison into a method of the key:

$ cat translate_cmp.py
class Key(str):
def __lt__(a, b):
return a + b < b + a

nums = ['3','30','34','32','9','5']
print(nums)
nums.sort(key=Key, reverse=True)
print(nums)
print("".join(nums))

$ python3 translate_cmp.py
['3', '30', '34', '32', '9', '5']
['9', '5', '34', '3', '32', '30']
953433230

The above works because in CPython list.sort() currently uses only the <
operator; adding __gt__() and __eq__() to make this portable is
straightforward even if you do not use the functools.total_ordering class
decorator.


38016...@gmail.com

unread,
Oct 15, 2016, 1:31:01 PM10/15/16
to
在 2016年10月14日星期五 UTC-4下午7:35:08,38016...@gmail.com写道:
!!!!!I learn more new tricks in Python. Thank you all of you guys.
You are all very kind helpful and knowledgeable.

Mike H

unread,
Oct 24, 2023, 8:58:37 PM10/24/23
to
Is it possible to use lambda expression instead of defining a `Key` class? Something like `sorted(my_list, key = lambda x, y: x+y > y+x)`?

Chris Angelico

unread,
Oct 24, 2023, 10:12:36 PM10/24/23
to
On Wed, 25 Oct 2023 at 13:02, Mike H via Python-list
<pytho...@python.org> wrote:
> Is it possible to use lambda expression instead of defining a `Key` class? Something like `sorted(my_list, key = lambda x, y: x+y > y+x)`?

Look up functools.cmp_to_key.

ChrisA
0 new messages