简体   繁体   中英

What's the proper usage of cmp argument in built-in sorted function? [python 2.7]

I know there are three additional argument in sorted(): cmp, key, reverse

I know well how to use key and reverse. Key can fast sort a list which its content item have multiple subitems, and reverse simply reverse the result. for example:

>>> L1 = [('mama','1'),('papa','2'),('son','0')]
>>> L1 = sorted(L1, key = lambda x: x[1], reverse = True)
>>> L1
[('papa', '2'), ('mama', '1'), ('son', '0')]

But so far I didn't figure out how to use cmp properly, though read through the official document. Can anyone offer a nice use case for cmp argument in sorted()?

cmp parameter accepts a function as argument which takes two arguments. The function is expected to return whether the first item is greater than, lesser than or equal to the second item.

But using that will be highly inefficient, as it has to be called for every comparison.

Quoting from Python - Sorting wiki ,

Many constructs given in this HOWTO assume Python 2.4 or later. Before that, there was no sorted() builtin and list.sort() took no keyword arguments. Instead, all of the Py2.x versions supported a cmp parameter to handle user specified comparison functions.

In Py3.0, the cmp parameter was removed entirely (as part of a larger effort to simplify and unify the language, eliminating the conflict between rich comparisons and the __cmp__ methods).

In Py2.x, sort allowed an optional function which can be called for doing the comparisons. That function should take two arguments to be compared and then return a negative value for less-than, return zero if they are equal, or return a positive value for greater-than. For example, we can do:

 >>> def numeric_compare(x, y): return x - y >>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare) [1, 2, 3, 4, 5] 

Note: In Python 2.7, we have a function called functools.cmp_to_key which can be used to convert a cmp function to a key function.

According to Python Docs :

cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument

When you sort a set of elements, you compare them against each other (this depends on the sorting algorithm) to try to figure out in what order they will be sorted. Here is where cmp comes to action.

cmp was the original parameter used to provide a custom sort order, and was the only parameter until key was introduced in Python 2.4. key has always been recommended over cmp as more efficient (even prior to Python 2.4, the Decorate-Sort-Undecorate idiom was encouraged; the key parameter was introduced to simplify its implementation), and in Python 3 support for cmp was removed altogether.

As such, there is no separate use case for cmp ; it just co-existed with key for a while before going away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM