blob: 114300ef21050264f66d57c9a52a972f04dc88b7 [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>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00008.. example based on the PyModules FAQ entry by Aaron Watters <arw@pythonpros.com>
Georg Brandl116aa622007-08-15 14:28:22 +00009
10This module provides support for maintaining a list in sorted order without
11having to sort the list after each insertion. For long lists of items with
12expensive comparison operations, this can be an improvement over the more common
13approach. The module is called :mod:`bisect` because it uses a basic bisection
14algorithm to do its work. The source code may be most useful as a working
15example of the algorithm (the boundary conditions are already right!).
16
17The following functions are provided:
18
19
20.. function:: bisect_left(list, item[, lo[, hi]])
21
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.
28
Georg Brandl116aa622007-08-15 14:28:22 +000029
30.. function:: bisect_right(list, item[, lo[, hi]])
31
32 Similar to :func:`bisect_left`, but returns an insertion point which comes after
33 (to the right of) any existing entries of *item* in *list*.
34
Georg Brandl116aa622007-08-15 14:28:22 +000035
36.. function:: bisect(...)
37
38 Alias for :func:`bisect_right`.
39
40
41.. function:: insort_left(list, item[, lo[, hi]])
42
43 Insert *item* in *list* in sorted order. This is equivalent to
44 ``list.insert(bisect.bisect_left(list, item, lo, hi), item)``. This assumes
45 that *list* is already sorted.
46
Georg Brandl116aa622007-08-15 14:28:22 +000047
48.. function:: insort_right(list, item[, lo[, hi]])
49
50 Similar to :func:`insort_left`, but inserting *item* in *list* after any
51 existing entries of *item*.
52
Georg Brandl116aa622007-08-15 14:28:22 +000053
54.. function:: insort(...)
55
56 Alias for :func:`insort_right`.
57
58
59Examples
60--------
61
62.. _bisect-example:
63
64The :func:`bisect` function is generally useful for categorizing numeric data.
65This example uses :func:`bisect` to look up a letter grade for an exam total
66(say) based on a set of ordered numeric breakpoints: 85 and up is an 'A', 75..84
67is a 'B', etc. ::
68
69 >>> grades = "FEDCBA"
70 >>> breakpoints = [30, 44, 66, 75, 85]
71 >>> from bisect import bisect
72 >>> def grade(total):
73 ... return grades[bisect(breakpoints, total)]
74 ...
75 >>> grade(66)
76 'C'
77 >>> map(grade, [33, 99, 77, 44, 12, 88])
78 ['E', 'A', 'B', 'D', 'F', 'A']
79
80