What Is The Right Python Idiom For Sorting By A Single Criterion (field Or Key)?
Solution 1:
In modern Python, the best way is to use list.sort
or sorted
with key
argument.
When you pass key
argument, sorting function, instead of comparing elements directly, compares whatever key
returned for them.
So, you can pass as key
any callable that takes element to be sorted as single positional argument, and returns what the element should be sorted by.
The callable will be called once for each element.
Some simple examples:
list_ = ['B', 'aaa', 'CC']
sorted(list_)
=> ['B', 'CC', 'aaa']
# case-insensitive sorting
sorted(list_, key=str.lower)
=> ['aaa', 'B', 'CC']
# sorting by length
sorted(list_, key=len)
=> ['B', 'CC', 'aaa']
Sorting with key
is roughly equivalent to Decorate-Sort-Undecorate pattern:
def decorate_sort_undecorate(iterable, key):
# 1: decorate:
decorated = [(key(elem), index, elem) forindex, elem in enumerate(iterable)]
# 2: sort:
decorated.sort()
# 3: undecorate: return [elem for key_, index, elem in decorated]
This creates temporary list (decorated
) of 3-element tuples,
which have form: (key_, index, elem)
, where key_ = key(elem)
. Then, decorated
list is sorted.
Tuples are compared by first non-equal element. This is key_
, or if key_
s are equal, index
. Because there are no equal indexes, elements are never directly compared.
At the end, elements are extracted from decorated
into new list, which is returned.
Random thoughts:
- Order of sorted elements can be reversed using
reverse=True
, lambda
s and functions fromoperator
module are often passed askey
,- before 2.4, there were no
key
parameter. There were only Decorate-Sort-Undecorate pattern and slowcmp
parameter, - Sorting a Python list by two criteria.
Post a Comment for "What Is The Right Python Idiom For Sorting By A Single Criterion (field Or Key)?"