Debian Codegraph @

cpython27/Doc/howto/sorting.rst

raw
1 .. _sortinghowto:
2
3 Sorting HOW TO
4 **************
5
6 :Author: Andrew Dalke and Raymond Hettinger
7 :Release: 0.1
8
9
10 Python lists have a built-in :meth:`list.sort` method that modifies the list
11 in-place. There is also a :func:`sorted` built-in function that builds a new
12 sorted list from an iterable.
13
14 In this document, we explore the various techniques for sorting data using Python.
15
16
17 Sorting Basics
18 ==============
19
20 A simple ascending sort is very easy: just call the :func:`sorted` function. It
21 returns a new sorted list::
22
23 >>> sorted([5, 2, 3, 1, 4])
24 [1, 2, 3, 4, 5]
25
26 You can also use the :meth:`list.sort` method of a list. It modifies the list
27 in-place (and returns ``None`` to avoid confusion). Usually it's less convenient
28 than :func:`sorted` - but if you don't need the original list, it's slightly
29 more efficient.
30
31 >>> a = [5, 2, 3, 1, 4]
32 >>> a.sort()
33 >>> a
34 [1, 2, 3, 4, 5]
35
36 Another difference is that the :meth:`list.sort` method is only defined for
37 lists. In contrast, the :func:`sorted` function accepts any iterable.
38
39 >>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
40 [1, 2, 3, 4, 5]
41
42 Key Functions
43 =============
44
45 Starting with Python 2.4, both :meth:`list.sort` and :func:`sorted` added a
46 *key* parameter to specify a function to be called on each list element prior to
47 making comparisons.
48
49 For example, here's a case-insensitive string comparison:
50
51 >>> sorted("This is a test string from Andrew".split(), key=str.lower)
52 ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
53
54 The value of the *key* parameter should be a function that takes a single argument
55 and returns a key to use for sorting purposes. This technique is fast because
56 the key function is called exactly once for each input record.
57
58 A common pattern is to sort complex objects using some of the object's indices
59 as keys. For example:
60
61 >>> student_tuples = [
62 ... ('john', 'A', 15),
63 ... ('jane', 'B', 12),
64 ... ('dave', 'B', 10),
65 ... ]
66 >>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
67 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
68
69 The same technique works for objects with named attributes. For example:
70
71 >>> class Student:
72 ... def __init__(self, name, grade, age):
73 ... self.name = name
74 ... self.grade = grade
75 ... self.age = age
76 ... def __repr__(self):
77 ... return repr((self.name, self.grade, self.age))
78
79 >>> student_objects = [
80 ... Student('john', 'A', 15),
81 ... Student('jane', 'B', 12),
82 ... Student('dave', 'B', 10),
83 ... ]
84 >>> sorted(student_objects, key=lambda student: student.age) # sort by age
85 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
86
87 Operator Module Functions
88 =========================
89
90 The key-function patterns shown above are very common, so Python provides
91 convenience functions to make accessor functions easier and faster. The operator
92 module has :func:`operator.itemgetter`, :func:`operator.attrgetter`, and
93 starting in Python 2.5 an :func:`operator.methodcaller` function.
94
95 Using those functions, the above examples become simpler and faster:
96
97 >>> from operator import itemgetter, attrgetter
98
99 >>> sorted(student_tuples, key=itemgetter(2))
100 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
101
102 >>> sorted(student_objects, key=attrgetter('age'))
103 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
104
105 The operator module functions allow multiple levels of sorting. For example, to
106 sort by *grade* then by *age*:
107
108 >>> sorted(student_tuples, key=itemgetter(1,2))
109 [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
110
111 >>> sorted(student_objects, key=attrgetter('grade', 'age'))
112 [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
113
114 The :func:`operator.methodcaller` function makes method calls with fixed
115 parameters for each object being sorted. For example, the :meth:`str.count`
116 method could be used to compute message priority by counting the
117 number of exclamation marks in a message:
118
119 >>> from operator import methodcaller
120 >>> messages = ['critical!!!', 'hurry!', 'standby', 'immediate!!']
121 >>> sorted(messages, key=methodcaller('count', '!'))
122 ['standby', 'hurry!', 'immediate!!', 'critical!!!']
123
124 Ascending and Descending
125 ========================
126
127 Both :meth:`list.sort` and :func:`sorted` accept a *reverse* parameter with a
128 boolean value. This is used to flag descending sorts. For example, to get the
129 student data in reverse *age* order:
130
131 >>> sorted(student_tuples, key=itemgetter(2), reverse=True)
132 [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
133
134 >>> sorted(student_objects, key=attrgetter('age'), reverse=True)
135 [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
136
137 Sort Stability and Complex Sorts
138 ================================
139
140 Starting with Python 2.2, sorts are guaranteed to be `stable
141 <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`_\. That means that
142 when multiple records have the same key, their original order is preserved.
143
144 >>> data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
145 >>> sorted(data, key=itemgetter(0))
146 [('blue', 1), ('blue', 2), ('red', 1), ('red', 2)]
147
148 Notice how the two records for *blue* retain their original order so that
149 ``('blue', 1)`` is guaranteed to precede ``('blue', 2)``.
150
151 This wonderful property lets you build complex sorts in a series of sorting
152 steps. For example, to sort the student data by descending *grade* and then
153 ascending *age*, do the *age* sort first and then sort again using *grade*:
154
155 >>> s = sorted(student_objects, key=attrgetter('age')) # sort on secondary key
156 >>> sorted(s, key=attrgetter('grade'), reverse=True) # now sort on primary key, descending
157 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
158
159 The `Timsort <https://en.wikipedia.org/wiki/Timsort>`_ algorithm used in Python
160 does multiple sorts efficiently because it can take advantage of any ordering
161 already present in a dataset.
162
163 The Old Way Using Decorate-Sort-Undecorate
164 ==========================================
165
166 This idiom is called Decorate-Sort-Undecorate after its three steps:
167
168 * First, the initial list is decorated with new values that control the sort order.
169
170 * Second, the decorated list is sorted.
171
172 * Finally, the decorations are removed, creating a list that contains only the
173 initial values in the new order.
174
175 For example, to sort the student data by *grade* using the DSU approach:
176
177 >>> decorated = [(student.grade, i, student) for i, student in enumerate(student_objects)]
178 >>> decorated.sort()
179 >>> [student for grade, i, student in decorated] # undecorate
180 [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
181
182 This idiom works because tuples are compared lexicographically; the first items
183 are compared; if they are the same then the second items are compared, and so
184 on.
185
186 It is not strictly necessary in all cases to include the index *i* in the
187 decorated list, but including it gives two benefits:
188
189 * The sort is stable -- if two items have the same key, their order will be
190 preserved in the sorted list.
191
192 * The original items do not have to be comparable because the ordering of the
193 decorated tuples will be determined by at most the first two items. So for
194 example the original list could contain complex numbers which cannot be sorted
195 directly.
196
197 Another name for this idiom is
198 `Schwartzian transform <https://en.wikipedia.org/wiki/Schwartzian_transform>`_\,
199 after Randal L. Schwartz, who popularized it among Perl programmers.
200
201 For large lists and lists where the comparison information is expensive to
202 calculate, and Python versions before 2.4, DSU is likely to be the fastest way
203 to sort the list. For 2.4 and later, key functions provide the same
204 functionality.
205
206 The Old Way Using the *cmp* Parameter
207 =====================================
208
209 Many constructs given in this HOWTO assume Python 2.4 or later. Before that,
210 there was no :func:`sorted` builtin and :meth:`list.sort` took no keyword
211 arguments. Instead, all of the Py2.x versions supported a *cmp* parameter to
212 handle user specified comparison functions.
213
214 In Python 3, the *cmp* parameter was removed entirely (as part of a larger effort to
215 simplify and unify the language, eliminating the conflict between rich
216 comparisons and the :meth:`__cmp__` magic method).
217
218 In Python 2, :meth:`~list.sort` allowed an optional function which can be called for doing the
219 comparisons. That function should take two arguments to be compared and then
220 return a negative value for less-than, return zero if they are equal, or return
221 a positive value for greater-than. For example, we can do:
222
223 >>> def numeric_compare(x, y):
224 ... return x - y
225 >>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare) # doctest: +SKIP
226 [1, 2, 3, 4, 5]
227
228 Or you can reverse the order of comparison with:
229
230 >>> def reverse_numeric(x, y):
231 ... return y - x
232 >>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric) # doctest: +SKIP
233 [5, 4, 3, 2, 1]
234
235 When porting code from Python 2.x to 3.x, the situation can arise when you have
236 the user supplying a comparison function and you need to convert that to a key
237 function. The following wrapper makes that easy to do::
238
239 def cmp_to_key(mycmp):
240 'Convert a cmp= function into a key= function'
241 class K(object):
242 def __init__(self, obj, *args):
243 self.obj = obj
244 def __lt__(self, other):
245 return mycmp(self.obj, other.obj) < 0
246 def __gt__(self, other):
247 return mycmp(self.obj, other.obj) > 0
248 def __eq__(self, other):
249 return mycmp(self.obj, other.obj) == 0
250 def __le__(self, other):
251 return mycmp(self.obj, other.obj) <= 0
252 def __ge__(self, other):
253 return mycmp(self.obj, other.obj) >= 0
254 def __ne__(self, other):
255 return mycmp(self.obj, other.obj) != 0
256 return K
257
258 To convert to a key function, just wrap the old comparison function:
259
260 .. testsetup::
261
262 from functools import cmp_to_key
263
264 .. doctest::
265
266 >>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric))
267 [5, 4, 3, 2, 1]
268
269 In Python 2.7, the :func:`functools.cmp_to_key` function was added to the
270 functools module.
271
272 Odd and Ends
273 ============
274
275 * For locale aware sorting, use :func:`locale.strxfrm` for a key function or
276 :func:`locale.strcoll` for a comparison function.
277
278 * The *reverse* parameter still maintains sort stability (so that records with
279 equal keys retain their original order). Interestingly, that effect can be
280 simulated without the parameter by using the builtin :func:`reversed` function
281 twice:
282
283 >>> data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
284 >>> standard_way = sorted(data, key=itemgetter(0), reverse=True)
285 >>> double_reversed = list(reversed(sorted(reversed(data), key=itemgetter(0))))
286 >>> assert standard_way == double_reversed
287 >>> standard_way
288 [('red', 1), ('red', 2), ('blue', 1), ('blue', 2)]
289
290 * To create a standard sort order for a class, just add the appropriate rich
291 comparison methods:
292
293 >>> Student.__eq__ = lambda self, other: self.age == other.age
294 >>> Student.__ne__ = lambda self, other: self.age != other.age
295 >>> Student.__lt__ = lambda self, other: self.age < other.age
296 >>> Student.__le__ = lambda self, other: self.age <= other.age
297 >>> Student.__gt__ = lambda self, other: self.age > other.age
298 >>> Student.__ge__ = lambda self, other: self.age >= other.age
299 >>> sorted(student_objects)
300 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
301
302 For general purpose comparisons, the recommended approach is to define all six
303 rich comparison operators. The :func:`functools.total_ordering` class
304 decorator makes this easy to implement.
305
306 * Key functions need not depend directly on the objects being sorted. A key
307 function can also access external resources. For instance, if the student grades
308 are stored in a dictionary, they can be used to sort a separate list of student
309 names:
310
311 >>> students = ['dave', 'john', 'jane']
312 >>> grades = {'john': 'F', 'jane':'A', 'dave': 'C'}
313 >>> sorted(students, key=grades.__getitem__)
314 ['jane', 'dave', 'john']