2 :mod:`bisect` --- Array bisection algorithm
3 ===========================================
6 :synopsis: Array bisection algorithms for binary searching.
7 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8 .. example based on the PyModules FAQ entry by Aaron Watters <arw@pythonpros.com>
10 This module provides support for maintaining a list in sorted order without
11 having to sort the list after each insertion. For long lists of items with
12 expensive comparison operations, this can be an improvement over the more common
13 approach. The module is called :mod:`bisect` because it uses a basic bisection
14 algorithm to do its work. The source code may be most useful as a working
15 example of the algorithm (the boundary conditions are already right!).
17 The following functions are provided:
20 .. function:: bisect_left(list, item[, lo[, hi]])
22 Locate the proper insertion point for *item* in *list* to maintain sorted order.
23 The parameters *lo* and *hi* may be used to specify a subset of the list which
24 should be considered; by default the entire list is used. If *item* is already
25 present in *list*, the insertion point will be before (to the left of) any
26 existing entries. The return value is suitable for use as the first parameter
27 to ``list.insert()``. This assumes that *list* is already sorted.
32 .. function:: bisect_right(list, item[, lo[, hi]])
34 Similar to :func:`bisect_left`, but returns an insertion point which comes after
35 (to the right of) any existing entries of *item* in *list*.
40 .. function:: bisect(...)
42 Alias for :func:`bisect_right`.
45 .. function:: insort_left(list, item[, lo[, hi]])
47 Insert *item* in *list* in sorted order. This is equivalent to
48 ``list.insert(bisect.bisect_left(list, item, lo, hi), item)``. This assumes
49 that *list* is already sorted.
54 .. function:: insort_right(list, item[, lo[, hi]])
56 Similar to :func:`insort_left`, but inserting *item* in *list* after any
57 existing entries of *item*.
62 .. function:: insort(...)
64 Alias for :func:`insort_right`.
72 The :func:`bisect` function is generally useful for categorizing numeric data.
73 This example uses :func:`bisect` to look up a letter grade for an exam total
74 (say) based on a set of ordered numeric breakpoints: 85 and up is an 'A', 75..84
78 >>> breakpoints = [30, 44, 66, 75, 85]
79 >>> from bisect import bisect
81 ... return grades[bisect(breakpoints, total)]
85 >>> map(grade, [33, 99, 77, 44, 12, 88])
86 ['E', 'A', 'B', 'D', 'F', 'A']
88 Unlike the :func:`sorted` function, it does not make sense for the :func:`bisect`
89 functions to have *key* or *reversed* arguments because that would lead to an
90 inefficent design (successive calls to bisect functions would not "remember"
91 all of the previous key lookups).
93 Instead, it is better to search a list of precomputed keys to find the index
94 of the record in question::
96 >>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
97 >>> data.sort(key=lambda r: r[1])
98 >>> keys = [r[1] for r in data] # precomputed list of keys
99 >>> data[bisect_left(keys, 0)]
101 >>> data[bisect_left(keys, 1)]
103 >>> data[bisect_left(keys, 5)]
105 >>> data[bisect_left(keys, 8)]