Changeset 391 for python/trunk/Doc/library/bisect.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/bisect.rst
r2 r391 1 2 1 :mod:`bisect` --- Array bisection algorithm 3 2 =========================================== … … 6 5 :synopsis: Array bisection algorithms for binary searching. 7 6 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 7 .. sectionauthor:: Raymond Hettinger <python at rcn.com> 8 8 .. example based on the PyModules FAQ entry by Aaron Watters <arw@pythonpros.com> 9 10 .. versionadded:: 2.1 11 12 **Source code:** :source:`Lib/bisect.py` 13 14 -------------- 9 15 10 16 This module provides support for maintaining a list in sorted order without … … 18 24 19 25 20 .. function:: bisect_left( list, item[, lo[, hi]])26 .. function:: bisect_left(a, x, lo=0, hi=len(a)) 21 27 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 which24 should be considered; by default the entire list is used. If *item* is already25 present in *list*, the insertion point will be before (to the left of) any26 existing entries. The return value is suitable for use as the first parameter27 to ``list.insert()``. This assumes that *list* is already sorted.28 Locate the insertion point for *x* in *a* to maintain sorted order. 29 The parameters *lo* and *hi* may be used to specify a subset of the list 30 which should be considered; by default the entire list is used. If *x* is 31 already present in *a*, the insertion point will be before (to the left of) 32 any existing entries. The return value is suitable for use as the first 33 parameter to ``list.insert()`` assuming that *a* is already sorted. 28 34 29 .. versionadded:: 2.1 35 The returned insertion point *i* partitions the array *a* into two halves so 36 that ``all(val < x for val in a[lo:i])`` for the left side and 37 ``all(val >= x for val in a[i:hi])`` for the right side. 38 39 .. function:: bisect_right(a, x, lo=0, hi=len(a)) 40 bisect(a, x, lo=0, hi=len(a)) 41 42 Similar to :func:`bisect_left`, but returns an insertion point which comes 43 after (to the right of) any existing entries of *x* in *a*. 44 45 The returned insertion point *i* partitions the array *a* into two halves so 46 that ``all(val <= x for val in a[lo:i])`` for the left side and 47 ``all(val > x for val in a[i:hi])`` for the right side. 48 49 .. function:: insort_left(a, x, lo=0, hi=len(a)) 50 51 Insert *x* in *a* in sorted order. This is equivalent to 52 ``a.insert(bisect.bisect_left(a, x, lo, hi), x)`` assuming that *a* is 53 already sorted. Keep in mind that the O(log n) search is dominated by 54 the slow O(n) insertion step. 55 56 .. function:: insort_right(a, x, lo=0, hi=len(a)) 57 insort(a, x, lo=0, hi=len(a)) 58 59 Similar to :func:`insort_left`, but inserting *x* in *a* after any existing 60 entries of *x*. 61 62 .. seealso:: 63 64 `SortedCollection recipe 65 <http://code.activestate.com/recipes/577197-sortedcollection/>`_ that uses 66 bisect to build a full-featured collection class with straight-forward search 67 methods and support for a key-function. The keys are precomputed to save 68 unnecessary calls to the key function during searches. 30 69 31 70 32 .. function:: bisect_right(list, item[, lo[, hi]]) 71 Searching Sorted Lists 72 ---------------------- 33 73 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*. 74 The above :func:`bisect` functions are useful for finding insertion points but 75 can be tricky or awkward to use for common searching tasks. The following five 76 functions show how to transform them into the standard lookups for sorted 77 lists:: 36 78 37 .. versionadded:: 2.1 79 def index(a, x): 80 'Locate the leftmost value exactly equal to x' 81 i = bisect_left(a, x) 82 if i != len(a) and a[i] == x: 83 return i 84 raise ValueError 85 86 def find_lt(a, x): 87 'Find rightmost value less than x' 88 i = bisect_left(a, x) 89 if i: 90 return a[i-1] 91 raise ValueError 92 93 def find_le(a, x): 94 'Find rightmost value less than or equal to x' 95 i = bisect_right(a, x) 96 if i: 97 return a[i-1] 98 raise ValueError 99 100 def find_gt(a, x): 101 'Find leftmost value greater than x' 102 i = bisect_right(a, x) 103 if i != len(a): 104 return a[i] 105 raise ValueError 106 107 def find_ge(a, x): 108 'Find leftmost item greater than or equal to x' 109 i = bisect_left(a, x) 110 if i != len(a): 111 return a[i] 112 raise ValueError 38 113 39 114 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 67 Examples 68 -------- 115 Other Examples 116 -------------- 69 117 70 118 .. _bisect-example: 71 119 72 The :func:`bisect` function is generally useful for categorizing numeric data.73 This 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 75 is a 'B', etc. 120 The :func:`bisect` function can be useful for numeric table lookups. This 121 example uses :func:`bisect` to look up a letter grade for an exam score (say) 122 based on a set of ordered numeric breakpoints: 90 and up is an 'A', 80 to 89 is 123 a 'B', and so on:: 76 124 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'] 125 >>> def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'): 126 i = bisect(breakpoints, score) 127 return grades[i] 128 129 >>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]] 130 ['F', 'A', 'C', 'C', 'B', 'A', 'A'] 87 131 88 132 Unlike the :func:`sorted` function, it does not make sense for the :func:`bisect` 89 133 functions to have *key* or *reversed* arguments because that would lead to an 90 ineffic ent design (successive calls to bisect functions would not "remember"134 inefficient design (successive calls to bisect functions would not "remember" 91 135 all of the previous key lookups). 92 136 … … 105 149 >>> data[bisect_left(keys, 8)] 106 150 ('yellow', 8) 151
Note:
See TracChangeset
for help on using the changeset viewer.