Mailing List Archive

How to sort this without 'cmp=' in python 3?
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
Am 15.10.2016 um 01:33 schrieb 380162267qq@gmail.com:
> 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?

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

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

--
Robin Koch



--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
On Friday, October 14, 2016 at 4:35:08 PM UTC-7, 38016...@gmail.com wrote:
> 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

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))

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
On Friday, October 14, 2016 at 7:49:33 PM UTC-4, Robin Koch wrote:
> Am 15.10.2016 um 01:33 schrieb 380162267qq@gmail.com:
> > 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?
>
> https://docs.python.org/3/library/functools.html#functools.cmp_to_key
>
> | Transform an old-style comparison function to a key function.

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.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
380162267qq@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

--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
On Saturday, October 15, 2016 at 12:53:48 AM UTC+1, sohca...@gmail.com wrote:
> On Friday, October 14, 2016 at 4:35:08 PM UTC-7, 38016...@gmail.com wrote:
> > 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
>
> 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))

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'
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
380162267qq@gmail.com wrote:

> 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

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.


--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
在 2016年10月14日星期五 UTC-4下午7:35:08,38016...@gmail.com写道:
> 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

!!!!!I learn more new tricks in Python. Thank you all of you guys.
You are all very kind helpful and knowledgeable.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
On Saturday, October 15, 2016 at 12:27:42?AM UTC-7, Peter Otten wrote:
> 38016...@gmail.com wrote:
>
> > 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
> 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.
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)`?
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3? [ In reply to ]
On Wed, 25 Oct 2023 at 13:02, Mike H via Python-list
<python-list@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
--
https://mail.python.org/mailman/listinfo/python-list