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