@@ -35,8 +35,11 @@ The following functions are provided:
35
35
``all(val >= x for val in a[i : hi]) `` for the right side.
36
36
37
37
*key * specifies a :term: `key function ` of one argument that is used to
38
- extract a comparison key from each input element. The default value is
39
- ``None `` (compare the elements directly).
38
+ extract a comparison key from each element in the array. To support
39
+ searching complex records, the key function is not applied to the *x * value.
40
+
41
+ If *key * is ``None ``, the elements are compared directly with no
42
+ intervening function call.
40
43
41
44
.. versionchanged :: 3.10
42
45
Added the *key * parameter.
@@ -53,8 +56,11 @@ The following functions are provided:
53
56
``all(val > x for val in a[i : hi]) `` for the right side.
54
57
55
58
*key * specifies a :term: `key function ` of one argument that is used to
56
- extract a comparison key from each input element. The default value is
57
- ``None `` (compare the elements directly).
59
+ extract a comparison key from each element in the array. To support
60
+ searching complex records, the key function is not applied to the *x * value.
61
+
62
+ If *key * is ``None ``, the elements are compared directly with no
63
+ intervening function call.
58
64
59
65
.. versionchanged :: 3.10
60
66
Added the *key * parameter.
@@ -64,14 +70,13 @@ The following functions are provided:
64
70
65
71
Insert *x * in *a * in sorted order.
66
72
67
- *key * specifies a :term: `key function ` of one argument that is used to
68
- extract a comparison key from each input element. The default value is
69
- ``None `` (compare the elements directly).
70
-
71
73
This function first runs :func: `bisect_left ` to locate an insertion point.
72
74
Next, it runs the :meth: `insert ` method on *a * to insert *x * at the
73
75
appropriate position to maintain sort order.
74
76
77
+ To support inserting records in a table, the *key * function (if any) is
78
+ applied to *x * for the search step but not for the insertion step.
79
+
75
80
Keep in mind that the ``O(log n) `` search is dominated by the slow O(n)
76
81
insertion step.
77
82
@@ -85,14 +90,13 @@ The following functions are provided:
85
90
Similar to :func: `insort_left `, but inserting *x * in *a * after any existing
86
91
entries of *x *.
87
92
88
- *key * specifies a :term: `key function ` of one argument that is used to
89
- extract a comparison key from each input element. The default value is
90
- ``None `` (compare the elements directly).
91
-
92
93
This function first runs :func: `bisect_right ` to locate an insertion point.
93
94
Next, it runs the :meth: `insert ` method on *a * to insert *x * at the
94
95
appropriate position to maintain sort order.
95
96
97
+ To support inserting records in a table, the *key * function (if any) is
98
+ applied to *x * for the search step but not for the insertion step.
99
+
96
100
Keep in mind that the ``O(log n) `` search is dominated by the slow O(n)
97
101
insertion step.
98
102
@@ -194,8 +198,42 @@ a 'B', and so on::
194
198
>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
195
199
['F', 'A', 'C', 'C', 'B', 'A', 'A']
196
200
197
- One technique to avoid repeated calls to a key function is to search a list of
198
- precomputed keys to find the index of a record::
201
+ The :func: `bisect`function and :func:`insort ` functions also work with lists of
202
+ tuples. The *key * argument can serve to extract the field used for ordering
203
+ records in a table::
204
+
205
+ >>> from collections import namedtuple
206
+ >>> from operator import attrgetter
207
+ >>> from bisect import bisect, insort
208
+ >>> from pprint import pprint
209
+
210
+ >>> Movie = namedtuple('Movie', ('name', 'released', 'director'))
211
+
212
+ >>> movies = [
213
+ ... Movie('Jaws', 1975, 'Speilberg'),
214
+ ... Movie('Titanic', 1997, 'Cameron'),
215
+ ... Movie('The Birds', 1963, 'Hitchcock'),
216
+ ... Movie('Aliens', 1986, 'Scott')
217
+ ... ]
218
+
219
+ >>> # Find the first movie released on or after 1960
220
+ >>> by_year = attrgetter('released')
221
+ >>> movies.sort(key=by_year)
222
+ >>> movies[bisect(movies, 1960, key=by_year)]
223
+ Movie(name='The Birds', released=1963, director='Hitchcock')
224
+
225
+ >>> # Insert a movie while maintaining sort order
226
+ >>> romance = Movie('Love Story', 1970, 'Hiller')
227
+ >>> insort(movies, romance, key=by_year)
228
+ >>> pprint(movies)
229
+ [Movie(name='The Birds', released=1963, director='Hitchcock'),
230
+ Movie(name='Love Story', released=1970, director='Hiller'),
231
+ Movie(name='Jaws', released=1975, director='Speilberg'),
232
+ Movie(name='Aliens', released=1986, director='Scott'),
233
+ Movie(name='Titanic', released=1997, director='Cameron')]
234
+
235
+ If the key function is expensive, it is possible to avoid repeated function
236
+ calls by searching a list of precomputed keys to find the index of a record::
199
237
200
238
>>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
201
239
>>> data.sort(key=lambda r: r[1]) # Or use operator.itemgetter(1).
@@ -208,4 +246,3 @@ precomputed keys to find the index of a record::
208
246
('red', 5)
209
247
>>> data[bisect_left(keys, 8)]
210
248
('yellow', 8)
211
-
0 commit comments