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