rest25/library/bisect.rst => rest262/library/bisect.rst
f1
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>
n8- 
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
8+.. example based on the PyModules FAQ entry by Aaron Watters <arw@pythonpros.com>
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
71Examples
72--------
73
74.. _bisect-example:
75
76The :func:`bisect` function is generally useful for categorizing numeric data.
77This example uses :func:`bisect` to look up a letter grade for an exam total
78(say) based on a set of ordered numeric breakpoints: 85 and up is an 'A', 75..84
t79-is a 'B', etc. ::
t75+is a 'B', etc.
80
81   >>> grades = "FEDCBA"
82   >>> breakpoints = [30, 44, 66, 75, 85]
83   >>> from bisect import bisect
84   >>> def grade(total):
85   ...           return grades[bisect(breakpoints, total)]
86   ...
87   >>> grade(66)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op