Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/bisect.rst

    r2 r391  
    1 
    21:mod:`bisect` --- Array bisection algorithm
    32===========================================
     
    65   :synopsis: Array bisection algorithms for binary searching.
    76.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
     7.. sectionauthor:: Raymond Hettinger <python at rcn.com>
    88.. 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--------------
    915
    1016This module provides support for maintaining a list in sorted order without
     
    1824
    1925
    20 .. function:: bisect_left(list, item[, lo[, hi]])
     26.. function:: bisect_left(a, x, lo=0, hi=len(a))
    2127
    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   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.
    2834
    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.
    3069
    3170
    32 .. function:: bisect_right(list, item[, lo[, hi]])
     71Searching Sorted Lists
     72----------------------
    3373
    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*.
     74The above :func:`bisect` functions are useful for finding insertion points but
     75can be tricky or awkward to use for common searching tasks. The following five
     76functions show how to transform them into the standard lookups for sorted
     77lists::
    3678
    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
    38113
    39114
    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 --------
     115Other Examples
     116--------------
    69117
    70118.. _bisect-example:
    71119
    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.
     120The :func:`bisect` function can be useful for numeric table lookups. This
     121example uses :func:`bisect` to look up a letter grade for an exam score (say)
     122based on a set of ordered numeric breakpoints: 90 and up is an 'A', 80 to 89 is
     123a 'B', and so on::
    76124
    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']
    87131
    88132Unlike the :func:`sorted` function, it does not make sense for the :func:`bisect`
    89133functions to have *key* or *reversed* arguments because that would lead to an
    90 inefficent design (successive calls to bisect functions would not "remember"
     134inefficient design (successive calls to bisect functions would not "remember"
    91135all of the previous key lookups).
    92136
     
    105149    >>> data[bisect_left(keys, 8)]
    106150    ('yellow', 8)
     151
Note: See TracChangeset for help on using the changeset viewer.