mirror of https://github.com/python/cpython
bpo-39374: Updated sorting documentation (GH-18177)
This commit is contained in:
parent
4b09dc79f4
commit
8271441d8b
|
@ -43,16 +43,18 @@ Key Functions
|
||||||
=============
|
=============
|
||||||
|
|
||||||
Both :meth:`list.sort` and :func:`sorted` have a *key* parameter to specify a
|
Both :meth:`list.sort` and :func:`sorted` have a *key* parameter to specify a
|
||||||
function to be called on each list element prior to making comparisons.
|
function (or other callable) to be called on each list element prior to making
|
||||||
|
comparisons.
|
||||||
|
|
||||||
For example, here's a case-insensitive string comparison:
|
For example, here's a case-insensitive string comparison:
|
||||||
|
|
||||||
>>> sorted("This is a test string from Andrew".split(), key=str.lower)
|
>>> sorted("This is a test string from Andrew".split(), key=str.lower)
|
||||||
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
|
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
|
||||||
|
|
||||||
The value of the *key* parameter should be a function that takes a single argument
|
The value of the *key* parameter should be a function (or other callable) that
|
||||||
and returns a key to use for sorting purposes. This technique is fast because
|
takes a single argument and returns a key to use for sorting purposes. This
|
||||||
the key function is called exactly once for each input record.
|
technique is fast because the key function is called exactly once for each
|
||||||
|
input record.
|
||||||
|
|
||||||
A common pattern is to sort complex objects using some of the object's indices
|
A common pattern is to sort complex objects using some of the object's indices
|
||||||
as keys. For example:
|
as keys. For example:
|
||||||
|
|
Loading…
Reference in New Issue