Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
87c9d6cf
Kaydet (Commit)
87c9d6cf
authored
Agu 07, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Improve the docs for bisect to cover common searching tasks.
üst
1b669962
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
3 deletions
+57
-3
bisect.rst
Doc/library/bisect.rst
+57
-3
No files found.
Doc/library/bisect.rst
Dosyayı görüntüle @
87c9d6cf
...
...
@@ -39,6 +39,9 @@ The following functions are provided:
``a.insert(bisect.bisect_left(a, x, lo, hi), x)``. This assumes that *a* is
already sorted.
Also note that while the fast search step is O(log n), the slower insertion
step is O(n), so the overall operation is slow.
.. function:: insort_right(a, x, lo=0, hi=len(a))
insort(a, x, lo=0, hi=len(a))
...
...
@@ -46,9 +49,53 @@ The following functions are provided:
Similar to :func:`insort_left`, but inserting *x* in *a* after any existing
entries of *x*.
Examples
--------
Also note that while the fast search step is O(log n), the slower insertion
step is O(n), so the overall operation is slow.
Searching Sorted Lists
----------------------
The above :func:`bisect` functions are useful for finding insertion points, but
can be tricky or awkward to use for common searching tasks. The following three
functions show how to transform them into the standard lookups for sorted
lists::
def find(a, key):
'''Find item with a key-value equal to key.
Raise ValueError if no such item exists.
'''
i = bisect_left(a, key)
if i < len(a) and a[i] == key:
return a[i]
raise ValueError('No item found with key equal to: %r' % (key,))
def find_le(a, key):
'''Find largest item with a key-value less-than or equal to key.
Raise ValueError if no such item exists.
If multiple key-values are equal, return the leftmost.
'''
i = bisect_left(a, key)
if i < len(a) and a[i] == key:
return a[i]
if i == 0:
raise ValueError('No item found with key at or below: %r' % (key,))
return a[i-1]
def find_ge(a, key):
'''Find smallest item with a key-value greater-than or equal to key.
Raise ValueError if no such item exists.
If multiple key-values are equal, return the leftmost.
'''
i = bisect_left(a, key)
if i == len(a):
raise ValueError('No item found with key at or above: %r' % (key,))
return a[i]
Other Examples
--------------
.. _bisect-example:
...
...
@@ -87,3 +134,10 @@ of the record in question::
('red', 5)
>>> data[bisect_left(keys, 8)]
('yellow', 8)
.. seealso::
`SortedCollection recipe
<http://code.activestate.com/recipes/577197-sortedcollection/>`_ that
encapsulates precomputed keys, allowing straight-forward insertion and
searching using a *key* function.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment