1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>8.6. bisect — Array bisection algorithm — Python 3.5.1 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.1 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
<link rel="up" title="8. Data Types" href="datatypes.html" />
34
<link rel="next" title="8.7. array — Efficient arrays of numeric values" href="array.html" />
35
<link rel="prev" title="8.5. heapq — Heap queue algorithm" href="heapq.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="array.html" title="8.7. array — Efficient arrays of numeric values"
55
accesskey="N">next</a> |</li>
57
<a href="heapq.html" title="8.5. heapq — Heap queue algorithm"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">3.5.1</span>
64
<a href="../index.html">Documentation </a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-bisect">
78
<span id="bisect-array-bisection-algorithm"></span><h1>8.6. <a class="reference internal" href="#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-mod docutils literal"><span class="pre">bisect</span></code></a> — Array bisection algorithm<a class="headerlink" href="#module-bisect" title="Permalink to this headline">¶</a></h1>
79
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/bisect.py">Lib/bisect.py</a></p>
80
<hr class="docutils" />
81
<p>This module provides support for maintaining a list in sorted order without
82
having to sort the list after each insertion. For long lists of items with
83
expensive comparison operations, this can be an improvement over the more common
84
approach. The module is called <a class="reference internal" href="#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-mod docutils literal"><span class="pre">bisect</span></code></a> because it uses a basic bisection
85
algorithm to do its work. The source code may be most useful as a working
86
example of the algorithm (the boundary conditions are already right!).</p>
87
<p>The following functions are provided:</p>
89
<dt id="bisect.bisect_left">
90
<code class="descclassname">bisect.</code><code class="descname">bisect_left</code><span class="sig-paren">(</span><em>a</em>, <em>x</em>, <em>lo=0</em>, <em>hi=len(a)</em><span class="sig-paren">)</span><a class="headerlink" href="#bisect.bisect_left" title="Permalink to this definition">¶</a></dt>
91
<dd><p>Locate the insertion point for <em>x</em> in <em>a</em> to maintain sorted order.
92
The parameters <em>lo</em> and <em>hi</em> may be used to specify a subset of the list
93
which should be considered; by default the entire list is used. If <em>x</em> is
94
already present in <em>a</em>, the insertion point will be before (to the left of)
95
any existing entries. The return value is suitable for use as the first
96
parameter to <code class="docutils literal"><span class="pre">list.insert()</span></code> assuming that <em>a</em> is already sorted.</p>
97
<p>The returned insertion point <em>i</em> partitions the array <em>a</em> into two halves so
98
that <code class="docutils literal"><span class="pre">all(val</span> <span class="pre"><</span> <span class="pre">x</span> <span class="pre">for</span> <span class="pre">val</span> <span class="pre">in</span> <span class="pre">a[lo:i])</span></code> for the left side and
99
<code class="docutils literal"><span class="pre">all(val</span> <span class="pre">>=</span> <span class="pre">x</span> <span class="pre">for</span> <span class="pre">val</span> <span class="pre">in</span> <span class="pre">a[i:hi])</span></code> for the right side.</p>
102
<dl class="function">
103
<dt id="bisect.bisect_right">
104
<code class="descclassname">bisect.</code><code class="descname">bisect_right</code><span class="sig-paren">(</span><em>a</em>, <em>x</em>, <em>lo=0</em>, <em>hi=len(a)</em><span class="sig-paren">)</span><a class="headerlink" href="#bisect.bisect_right" title="Permalink to this definition">¶</a></dt>
105
<dt id="bisect.bisect">
106
<code class="descclassname">bisect.</code><code class="descname">bisect</code><span class="sig-paren">(</span><em>a</em>, <em>x</em>, <em>lo=0</em>, <em>hi=len(a)</em><span class="sig-paren">)</span><a class="headerlink" href="#bisect.bisect" title="Permalink to this definition">¶</a></dt>
107
<dd><p>Similar to <a class="reference internal" href="#bisect.bisect_left" title="bisect.bisect_left"><code class="xref py py-func docutils literal"><span class="pre">bisect_left()</span></code></a>, but returns an insertion point which comes
108
after (to the right of) any existing entries of <em>x</em> in <em>a</em>.</p>
109
<p>The returned insertion point <em>i</em> partitions the array <em>a</em> into two halves so
110
that <code class="docutils literal"><span class="pre">all(val</span> <span class="pre"><=</span> <span class="pre">x</span> <span class="pre">for</span> <span class="pre">val</span> <span class="pre">in</span> <span class="pre">a[lo:i])</span></code> for the left side and
111
<code class="docutils literal"><span class="pre">all(val</span> <span class="pre">></span> <span class="pre">x</span> <span class="pre">for</span> <span class="pre">val</span> <span class="pre">in</span> <span class="pre">a[i:hi])</span></code> for the right side.</p>
114
<dl class="function">
115
<dt id="bisect.insort_left">
116
<code class="descclassname">bisect.</code><code class="descname">insort_left</code><span class="sig-paren">(</span><em>a</em>, <em>x</em>, <em>lo=0</em>, <em>hi=len(a)</em><span class="sig-paren">)</span><a class="headerlink" href="#bisect.insort_left" title="Permalink to this definition">¶</a></dt>
117
<dd><p>Insert <em>x</em> in <em>a</em> in sorted order. This is equivalent to
118
<code class="docutils literal"><span class="pre">a.insert(bisect.bisect_left(a,</span> <span class="pre">x,</span> <span class="pre">lo,</span> <span class="pre">hi),</span> <span class="pre">x)</span></code> assuming that <em>a</em> is
119
already sorted. Keep in mind that the O(log n) search is dominated by
120
the slow O(n) insertion step.</p>
123
<dl class="function">
124
<dt id="bisect.insort_right">
125
<code class="descclassname">bisect.</code><code class="descname">insort_right</code><span class="sig-paren">(</span><em>a</em>, <em>x</em>, <em>lo=0</em>, <em>hi=len(a)</em><span class="sig-paren">)</span><a class="headerlink" href="#bisect.insort_right" title="Permalink to this definition">¶</a></dt>
126
<dt id="bisect.insort">
127
<code class="descclassname">bisect.</code><code class="descname">insort</code><span class="sig-paren">(</span><em>a</em>, <em>x</em>, <em>lo=0</em>, <em>hi=len(a)</em><span class="sig-paren">)</span><a class="headerlink" href="#bisect.insort" title="Permalink to this definition">¶</a></dt>
128
<dd><p>Similar to <a class="reference internal" href="#bisect.insort_left" title="bisect.insort_left"><code class="xref py py-func docutils literal"><span class="pre">insort_left()</span></code></a>, but inserting <em>x</em> in <em>a</em> after any existing
129
entries of <em>x</em>.</p>
132
<div class="admonition seealso">
133
<p class="first admonition-title">See also</p>
134
<p class="last"><a class="reference external" href="http://code.activestate.com/recipes/577197-sortedcollection/">SortedCollection recipe</a> that uses
135
bisect to build a full-featured collection class with straight-forward search
136
methods and support for a key-function. The keys are precomputed to save
137
unnecessary calls to the key function during searches.</p>
139
<div class="section" id="searching-sorted-lists">
140
<h2>8.6.1. Searching Sorted Lists<a class="headerlink" href="#searching-sorted-lists" title="Permalink to this headline">¶</a></h2>
141
<p>The above <a class="reference internal" href="#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-func docutils literal"><span class="pre">bisect()</span></code></a> functions are useful for finding insertion points but
142
can be tricky or awkward to use for common searching tasks. The following five
143
functions show how to transform them into the standard lookups for sorted
145
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
146
<span class="s1">'Locate the leftmost value exactly equal to x'</span>
147
<span class="n">i</span> <span class="o">=</span> <span class="n">bisect_left</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
148
<span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="ow">and</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="n">x</span><span class="p">:</span>
149
<span class="k">return</span> <span class="n">i</span>
150
<span class="k">raise</span> <span class="ne">ValueError</span>
152
<span class="k">def</span> <span class="nf">find_lt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
153
<span class="s1">'Find rightmost value less than x'</span>
154
<span class="n">i</span> <span class="o">=</span> <span class="n">bisect_left</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
155
<span class="k">if</span> <span class="n">i</span><span class="p">:</span>
156
<span class="k">return</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
157
<span class="k">raise</span> <span class="ne">ValueError</span>
159
<span class="k">def</span> <span class="nf">find_le</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
160
<span class="s1">'Find rightmost value less than or equal to x'</span>
161
<span class="n">i</span> <span class="o">=</span> <span class="n">bisect_right</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
162
<span class="k">if</span> <span class="n">i</span><span class="p">:</span>
163
<span class="k">return</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
164
<span class="k">raise</span> <span class="ne">ValueError</span>
166
<span class="k">def</span> <span class="nf">find_gt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
167
<span class="s1">'Find leftmost value greater than x'</span>
168
<span class="n">i</span> <span class="o">=</span> <span class="n">bisect_right</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
169
<span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>
170
<span class="k">return</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
171
<span class="k">raise</span> <span class="ne">ValueError</span>
173
<span class="k">def</span> <span class="nf">find_ge</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
174
<span class="s1">'Find leftmost item greater than or equal to x'</span>
175
<span class="n">i</span> <span class="o">=</span> <span class="n">bisect_left</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
176
<span class="k">if</span> <span class="n">i</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">):</span>
177
<span class="k">return</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
178
<span class="k">raise</span> <span class="ne">ValueError</span>
182
<div class="section" id="other-examples">
183
<h2>8.6.2. Other Examples<a class="headerlink" href="#other-examples" title="Permalink to this headline">¶</a></h2>
184
<p id="bisect-example">The <a class="reference internal" href="#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-func docutils literal"><span class="pre">bisect()</span></code></a> function can be useful for numeric table lookups. This
185
example uses <a class="reference internal" href="#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-func docutils literal"><span class="pre">bisect()</span></code></a> to look up a letter grade for an exam score (say)
186
based on a set of ordered numeric breakpoints: 90 and up is an ‘A’, 80 to 89 is
187
a ‘B’, and so on:</p>
188
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">grade</span><span class="p">(</span><span class="n">score</span><span class="p">,</span> <span class="n">breakpoints</span><span class="o">=</span><span class="p">[</span><span class="mi">60</span><span class="p">,</span> <span class="mi">70</span><span class="p">,</span> <span class="mi">80</span><span class="p">,</span> <span class="mi">90</span><span class="p">],</span> <span class="n">grades</span><span class="o">=</span><span class="s1">'FDCBA'</span><span class="p">):</span>
189
<span class="gp">... </span> <span class="n">i</span> <span class="o">=</span> <span class="n">bisect</span><span class="p">(</span><span class="n">breakpoints</span><span class="p">,</span> <span class="n">score</span><span class="p">)</span>
190
<span class="gp">... </span> <span class="k">return</span> <span class="n">grades</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
191
<span class="gp">...</span>
192
<span class="gp">>>> </span><span class="p">[</span><span class="n">grade</span><span class="p">(</span><span class="n">score</span><span class="p">)</span> <span class="k">for</span> <span class="n">score</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">33</span><span class="p">,</span> <span class="mi">99</span><span class="p">,</span> <span class="mi">77</span><span class="p">,</span> <span class="mi">70</span><span class="p">,</span> <span class="mi">89</span><span class="p">,</span> <span class="mi">90</span><span class="p">,</span> <span class="mi">100</span><span class="p">]]</span>
193
<span class="go">['F', 'A', 'C', 'C', 'B', 'A', 'A']</span>
196
<p>Unlike the <a class="reference internal" href="functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal"><span class="pre">sorted()</span></code></a> function, it does not make sense for the <a class="reference internal" href="#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-func docutils literal"><span class="pre">bisect()</span></code></a>
197
functions to have <em>key</em> or <em>reversed</em> arguments because that would lead to an
198
inefficient design (successive calls to bisect functions would not “remember”
199
all of the previous key lookups).</p>
200
<p>Instead, it is better to search a list of precomputed keys to find the index
201
of the record in question:</p>
202
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">'red'</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="p">(</span><span class="s1">'blue'</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">'yellow'</span><span class="p">,</span> <span class="mi">8</span><span class="p">),</span> <span class="p">(</span><span class="s1">'black'</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]</span>
203
<span class="gp">>>> </span><span class="n">data</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">r</span><span class="p">:</span> <span class="n">r</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
204
<span class="gp">>>> </span><span class="n">keys</span> <span class="o">=</span> <span class="p">[</span><span class="n">r</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">data</span><span class="p">]</span> <span class="c1"># precomputed list of keys</span>
205
<span class="gp">>>> </span><span class="n">data</span><span class="p">[</span><span class="n">bisect_left</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]</span>
206
<span class="go">('black', 0)</span>
207
<span class="gp">>>> </span><span class="n">data</span><span class="p">[</span><span class="n">bisect_left</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="mi">1</span><span class="p">)]</span>
208
<span class="go">('blue', 1)</span>
209
<span class="gp">>>> </span><span class="n">data</span><span class="p">[</span><span class="n">bisect_left</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="mi">5</span><span class="p">)]</span>
210
<span class="go">('red', 5)</span>
211
<span class="gp">>>> </span><span class="n">data</span><span class="p">[</span><span class="n">bisect_left</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="mi">8</span><span class="p">)]</span>
212
<span class="go">('yellow', 8)</span>
222
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
223
<div class="sphinxsidebarwrapper">
224
<h3><a href="../contents.html">Table Of Contents</a></h3>
226
<li><a class="reference internal" href="#">8.6. <code class="docutils literal"><span class="pre">bisect</span></code> — Array bisection algorithm</a><ul>
227
<li><a class="reference internal" href="#searching-sorted-lists">8.6.1. Searching Sorted Lists</a></li>
228
<li><a class="reference internal" href="#other-examples">8.6.2. Other Examples</a></li>
233
<h4>Previous topic</h4>
234
<p class="topless"><a href="heapq.html"
235
title="previous chapter">8.5. <code class="docutils literal"><span class="pre">heapq</span></code> — Heap queue algorithm</a></p>
237
<p class="topless"><a href="array.html"
238
title="next chapter">8.7. <code class="docutils literal"><span class="pre">array</span></code> — Efficient arrays of numeric values</a></p>
240
<ul class="this-page-menu">
241
<li><a href="../bugs.html">Report a Bug</a></li>
242
<li><a href="../_sources/library/bisect.txt"
243
rel="nofollow">Show Source</a></li>
246
<div id="searchbox" style="display: none" role="search">
247
<h3>Quick search</h3>
248
<form class="search" action="../search.html" method="get">
249
<input type="text" name="q" />
250
<input type="submit" value="Go" />
251
<input type="hidden" name="check_keywords" value="yes" />
252
<input type="hidden" name="area" value="default" />
254
<p class="searchtip" style="font-size: 90%">
255
Enter search terms or a module, class or function name.
258
<script type="text/javascript">$('#searchbox').show(0);</script>
261
<div class="clearer"></div>
263
<div class="related" role="navigation" aria-label="related navigation">
266
<li class="right" style="margin-right: 10px">
267
<a href="../genindex.html" title="General Index"
270
<a href="../py-modindex.html" title="Python Module Index"
273
<a href="array.html" title="8.7. array — Efficient arrays of numeric values"
276
<a href="heapq.html" title="8.5. heapq — Heap queue algorithm"
278
<li><img src="../_static/py.png" alt=""
279
style="vertical-align: middle; margin-top: -1px"/></li>
280
<li><a href="https://www.python.org/">Python</a> »</li>
282
<span class="version_switcher_placeholder">3.5.1</span>
283
<a href="../index.html">Documentation </a> »
286
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
287
<li class="nav-item nav-item-2"><a href="datatypes.html" >8. Data Types</a> »</li>
291
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
293
The Python Software Foundation is a non-profit corporation.
294
<a href="https://www.python.org/psf/donations/">Please donate.</a>
296
Last updated on Jan 22, 2016.
297
<a href="../bugs.html">Found a bug</a>?
299
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'