mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
[3.11] GH-104145: Use fully-qualified cross reference types for the bisect module (GH-104172) (#104295)
GH-104145: Use fully-qualified cross reference types for the bisect module (GH-104172)
(cherry picked from commit 76eef552f3
)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
parent
d54f6441ce
commit
8a6ff59e09
1 changed files with 12 additions and 10 deletions
|
@ -18,6 +18,8 @@ approach. The module is called :mod:`bisect` because it uses a basic bisection
|
||||||
algorithm to do its work. The source code may be most useful as a working
|
algorithm to do its work. The source code may be most useful as a working
|
||||||
example of the algorithm (the boundary conditions are already right!).
|
example of the algorithm (the boundary conditions are already right!).
|
||||||
|
|
||||||
|
.. _bisect functions:
|
||||||
|
|
||||||
The following functions are provided:
|
The following functions are provided:
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ The following functions are provided:
|
||||||
.. function:: bisect_right(a, x, lo=0, hi=len(a), *, key=None)
|
.. function:: bisect_right(a, x, lo=0, hi=len(a), *, key=None)
|
||||||
bisect(a, x, lo=0, hi=len(a), *, key=None)
|
bisect(a, x, lo=0, hi=len(a), *, key=None)
|
||||||
|
|
||||||
Similar to :func:`bisect_left`, but returns an insertion point which comes
|
Similar to :py:func:`~bisect.bisect_left`, but returns an insertion point which comes
|
||||||
after (to the right of) any existing entries of *x* in *a*.
|
after (to the right of) any existing entries of *x* in *a*.
|
||||||
|
|
||||||
The returned insertion point *i* partitions the array *a* into two halves so
|
The returned insertion point *i* partitions the array *a* into two halves so
|
||||||
|
@ -70,7 +72,7 @@ The following functions are provided:
|
||||||
|
|
||||||
Insert *x* in *a* in sorted order.
|
Insert *x* in *a* in sorted order.
|
||||||
|
|
||||||
This function first runs :func:`bisect_left` to locate an insertion point.
|
This function first runs :py:func:`~bisect.bisect_left` to locate an insertion point.
|
||||||
Next, it runs the :meth:`insert` method on *a* to insert *x* at the
|
Next, it runs the :meth:`insert` method on *a* to insert *x* at the
|
||||||
appropriate position to maintain sort order.
|
appropriate position to maintain sort order.
|
||||||
|
|
||||||
|
@ -87,10 +89,10 @@ The following functions are provided:
|
||||||
.. function:: insort_right(a, x, lo=0, hi=len(a), *, key=None)
|
.. function:: insort_right(a, x, lo=0, hi=len(a), *, key=None)
|
||||||
insort(a, x, lo=0, hi=len(a), *, key=None)
|
insort(a, x, lo=0, hi=len(a), *, key=None)
|
||||||
|
|
||||||
Similar to :func:`insort_left`, but inserting *x* in *a* after any existing
|
Similar to :py:func:`~bisect.insort_left`, but inserting *x* in *a* after any existing
|
||||||
entries of *x*.
|
entries of *x*.
|
||||||
|
|
||||||
This function first runs :func:`bisect_right` to locate an insertion point.
|
This function first runs :py:func:`~bisect.bisect_right` to locate an insertion point.
|
||||||
Next, it runs the :meth:`insert` method on *a* to insert *x* at the
|
Next, it runs the :meth:`insert` method on *a* to insert *x* at the
|
||||||
appropriate position to maintain sort order.
|
appropriate position to maintain sort order.
|
||||||
|
|
||||||
|
@ -120,7 +122,7 @@ thoughts in mind:
|
||||||
they are used. Consequently, if the search functions are used in a loop,
|
they are used. Consequently, if the search functions are used in a loop,
|
||||||
the key function may be called again and again on the same array elements.
|
the key function may be called again and again on the same array elements.
|
||||||
If the key function isn't fast, consider wrapping it with
|
If the key function isn't fast, consider wrapping it with
|
||||||
:func:`functools.cache` to avoid duplicate computations. Alternatively,
|
:py:func:`functools.cache` to avoid duplicate computations. Alternatively,
|
||||||
consider searching an array of precomputed keys to locate the insertion
|
consider searching an array of precomputed keys to locate the insertion
|
||||||
point (as shown in the examples section below).
|
point (as shown in the examples section below).
|
||||||
|
|
||||||
|
@ -140,7 +142,7 @@ thoughts in mind:
|
||||||
Searching Sorted Lists
|
Searching Sorted Lists
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
The above :func:`bisect` functions are useful for finding insertion points but
|
The above `bisect functions`_ are useful for finding insertion points but
|
||||||
can be tricky or awkward to use for common searching tasks. The following five
|
can be tricky or awkward to use for common searching tasks. The following five
|
||||||
functions show how to transform them into the standard lookups for sorted
|
functions show how to transform them into the standard lookups for sorted
|
||||||
lists::
|
lists::
|
||||||
|
@ -186,8 +188,8 @@ Examples
|
||||||
|
|
||||||
.. _bisect-example:
|
.. _bisect-example:
|
||||||
|
|
||||||
The :func:`bisect` function can be useful for numeric table lookups. This
|
The :py:func:`~bisect.bisect` function can be useful for numeric table lookups. This
|
||||||
example uses :func:`bisect` to look up a letter grade for an exam score (say)
|
example uses :py:func:`~bisect.bisect` to look up a letter grade for an exam score (say)
|
||||||
based on a set of ordered numeric breakpoints: 90 and up is an 'A', 80 to 89 is
|
based on a set of ordered numeric breakpoints: 90 and up is an 'A', 80 to 89 is
|
||||||
a 'B', and so on::
|
a 'B', and so on::
|
||||||
|
|
||||||
|
@ -198,8 +200,8 @@ a 'B', and so on::
|
||||||
>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
|
>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
|
||||||
['F', 'A', 'C', 'C', 'B', 'A', 'A']
|
['F', 'A', 'C', 'C', 'B', 'A', 'A']
|
||||||
|
|
||||||
The :func:`bisect` and :func:`insort` functions also work with lists of
|
The :py:func:`~bisect.bisect` and :py:func:`~bisect.insort` functions also work with
|
||||||
tuples. The *key* argument can serve to extract the field used for ordering
|
lists of tuples. The *key* argument can serve to extract the field used for ordering
|
||||||
records in a table::
|
records in a table::
|
||||||
|
|
||||||
>>> from collections import namedtuple
|
>>> from collections import namedtuple
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue