PageRenderTime 387ms CodeModel.GetById 153ms app.highlight 4ms RepoModel.GetById 227ms app.codeStats 1ms

/Doc/library/bisect.rst

http://unladen-swallow.googlecode.com/
ReStructuredText | 106 lines | 72 code | 34 blank | 0 comment | 0 complexity | d6a5ede4b66cc89b0b4b4975e65cf4c7 MD5 | raw file
  1
  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.. example based on the PyModules FAQ entry by Aaron Watters <arw@pythonpros.com>
  9
 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
 29   .. versionadded:: 2.1
 30
 31
 32.. function:: bisect_right(list, item[, lo[, hi]])
 33
 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*.
 36
 37   .. versionadded:: 2.1
 38
 39
 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
 51   .. versionadded:: 2.1
 52
 53
 54.. function:: insort_right(list, item[, lo[, hi]])
 55
 56   Similar to :func:`insort_left`, but inserting *item* in *list* after any
 57   existing entries of *item*.
 58
 59   .. versionadded:: 2.1
 60
 61
 62.. function:: insort(...)
 63
 64   Alias for :func:`insort_right`.
 65
 66
 67Examples
 68--------
 69
 70.. _bisect-example:
 71
 72The :func:`bisect` function is generally useful for categorizing numeric data.
 73This 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
 75is a 'B', etc.
 76
 77   >>> grades = "FEDCBA"
 78   >>> breakpoints = [30, 44, 66, 75, 85]
 79   >>> from bisect import bisect
 80   >>> def grade(total):
 81   ...           return grades[bisect(breakpoints, total)]
 82   ...
 83   >>> grade(66)
 84   'C'
 85   >>> map(grade, [33, 99, 77, 44, 12, 88])
 86   ['E', 'A', 'B', 'D', 'F', 'A']
 87
 88Unlike the :func:`sorted` function, it does not make sense for the :func:`bisect`
 89functions to have *key* or *reversed* arguments because that would lead to an
 90inefficent design (successive calls to bisect functions would not "remember"
 91all of the previous key lookups).
 92
 93Instead, it is better to search a list of precomputed keys to find the index
 94of the record in question::
 95
 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)]
100    ('black', 0)
101    >>> data[bisect_left(keys, 1)]
102    ('blue', 1)
103    >>> data[bisect_left(keys, 5)]
104    ('red', 5)
105    >>> data[bisect_left(keys, 8)]
106    ('yellow', 8)