blob: 10f72fb7216af7dd7116b7c14cfb2faaf792bbb0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`bisect` --- Array bisection algorithm
3===========================================
4
5.. module:: bisect
6 :synopsis: Array bisection algorithms for binary searching.
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
9
10.. % LaTeX produced by Fred L. Drake, Jr. <fdrake@acm.org>, with an
11.. % example based on the PyModules FAQ entry by Aaron Watters
12.. % <arw@pythonpros.com>.
13
14This module provides support for maintaining a list in sorted order without
15having to sort the list after each insertion. For long lists of items with
16expensive comparison operations, this can be an improvement over the more common
17approach. The module is called :mod:`bisect` because it uses a basic bisection
18algorithm to do its work. The source code may be most useful as a working
19example of the algorithm (the boundary conditions are already right!).
20
21The following functions are provided:
22
23
24.. function:: bisect_left(list, item[, lo[, hi]])
25
26 Locate the proper insertion point for *item* in *list* to maintain sorted order.
27 The parameters *lo* and *hi* may be used to specify a subset of the list which
28 should be considered; by default the entire list is used. If *item* is already
29 present in *list*, the insertion point will be before (to the left of) any
30 existing entries. The return value is suitable for use as the first parameter
31 to ``list.insert()``. This assumes that *list* is already sorted.
32
Georg Brandl116aa622007-08-15 14:28:22 +000033
34.. function:: bisect_right(list, item[, lo[, hi]])
35
36 Similar to :func:`bisect_left`, but returns an insertion point which comes after
37 (to the right of) any existing entries of *item* in *list*.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039
40.. function:: bisect(...)
41
42 Alias for :func:`bisect_right`.
43
44
45.. function:: insort_left(list, item[, lo[, hi]])
46
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.
50
Georg Brandl116aa622007-08-15 14:28:22 +000051
52.. function:: insort_right(list, item[, lo[, hi]])
53
54 Similar to :func:`insort_left`, but inserting *item* in *list* after any
55 existing entries of *item*.
56
Georg Brandl116aa622007-08-15 14:28:22 +000057
58.. function:: insort(...)
59
60 Alias for :func:`insort_right`.
61
62
63Examples
64--------
65
66.. _bisect-example:
67
68The :func:`bisect` function is generally useful for categorizing numeric data.
69This example uses :func:`bisect` to look up a letter grade for an exam total
70(say) based on a set of ordered numeric breakpoints: 85 and up is an 'A', 75..84
71is a 'B', etc. ::
72
73 >>> grades = "FEDCBA"
74 >>> breakpoints = [30, 44, 66, 75, 85]
75 >>> from bisect import bisect
76 >>> def grade(total):
77 ... return grades[bisect(breakpoints, total)]
78 ...
79 >>> grade(66)
80 'C'
81 >>> map(grade, [33, 99, 77, 44, 12, 88])
82 ['E', 'A', 'B', 'D', 'F', 'A']
83
84