Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-collections.html" /> | |
13 | <link rel="prev" href="module-whrandom.html" /> | |
14 | <link rel="parent" href="misc.html" /> | |
15 | <link rel="next" href="bisect-example.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.11 bisect -- Array bisection algorithm</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="5.10 whrandom " | |
25 | href="module-whrandom.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" | |
28 | href="misc.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="5.11.1 Examples" | |
31 | href="bisect-example.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-whrandom.html">5.10 whrandom </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="bisect-example.html">5.11.1 Examples</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0071100000000000000000"> | |
56 | 5.11 <tt class="module">bisect</tt> -- | |
57 | Array bisection algorithm</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-bisect"></A> | |
62 | ||
63 | <P> | |
64 | This module provides support for maintaining a list in sorted order | |
65 | without having to sort the list after each insertion. For long lists | |
66 | of items with expensive comparison operations, this can be an | |
67 | improvement over the more common approach. The module is called | |
68 | <tt class="module">bisect</tt> because it uses a basic bisection algorithm to do its | |
69 | work. The source code may be most useful as a working example of the | |
70 | algorithm (the boundary conditions are already right!). | |
71 | ||
72 | <P> | |
73 | The following functions are provided: | |
74 | ||
75 | <P> | |
76 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
77 | <td><nobr><b><tt id='l2h-1330' xml:id='l2h-1330' class="function">bisect_left</tt></b>(</nobr></td> | |
78 | <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
79 | <dd> | |
80 | Locate the proper insertion point for <var>item</var> in <var>list</var> to | |
81 | maintain sorted order. The parameters <var>lo</var> and <var>hi</var> may be | |
82 | used to specify a subset of the list which should be considered; by | |
83 | default the entire list is used. If <var>item</var> is already present | |
84 | in <var>list</var>, the insertion point will be before (to the left of) | |
85 | any existing entries. The return value is suitable for use as the | |
86 | first parameter to <code><var>list</var>.insert()</code>. This assumes that | |
87 | <var>list</var> is already sorted. | |
88 | ||
89 | <span class="versionnote">New in version 2.1.</span> | |
90 | ||
91 | </dl> | |
92 | ||
93 | <P> | |
94 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
95 | <td><nobr><b><tt id='l2h-1331' xml:id='l2h-1331' class="function">bisect_right</tt></b>(</nobr></td> | |
96 | <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
97 | <dd> | |
98 | Similar to <tt class="function">bisect_left()</tt>, but returns an insertion point | |
99 | which comes after (to the right of) any existing entries of | |
100 | <var>item</var> in <var>list</var>. | |
101 | ||
102 | <span class="versionnote">New in version 2.1.</span> | |
103 | ||
104 | </dl> | |
105 | ||
106 | <P> | |
107 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
108 | <td><nobr><b><tt id='l2h-1332' xml:id='l2h-1332' class="function">bisect</tt></b>(</nobr></td> | |
109 | <td><var>...</var>)</td></tr></table></dt> | |
110 | <dd> | |
111 | Alias for <tt class="function">bisect_right()</tt>. | |
112 | </dl> | |
113 | ||
114 | <P> | |
115 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
116 | <td><nobr><b><tt id='l2h-1333' xml:id='l2h-1333' class="function">insort_left</tt></b>(</nobr></td> | |
117 | <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
118 | <dd> | |
119 | Insert <var>item</var> in <var>list</var> in sorted order. This is equivalent | |
120 | to <code><var>list</var>.insert(bisect.bisect_left(<var>list</var>, <var>item</var>, | |
121 | <var>lo</var>, <var>hi</var>), <var>item</var>)</code>. This assumes that <var>list</var> is | |
122 | already sorted. | |
123 | ||
124 | <span class="versionnote">New in version 2.1.</span> | |
125 | ||
126 | </dl> | |
127 | ||
128 | <P> | |
129 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
130 | <td><nobr><b><tt id='l2h-1334' xml:id='l2h-1334' class="function">insort_right</tt></b>(</nobr></td> | |
131 | <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
132 | <dd> | |
133 | Similar to <tt class="function">insort_left()</tt>, but inserting <var>item</var> in | |
134 | <var>list</var> after any existing entries of <var>item</var>. | |
135 | ||
136 | <span class="versionnote">New in version 2.1.</span> | |
137 | ||
138 | </dl> | |
139 | ||
140 | <P> | |
141 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
142 | <td><nobr><b><tt id='l2h-1335' xml:id='l2h-1335' class="function">insort</tt></b>(</nobr></td> | |
143 | <td><var>...</var>)</td></tr></table></dt> | |
144 | <dd> | |
145 | Alias for <tt class="function">insort_right()</tt>. | |
146 | </dl> | |
147 | ||
148 | <P> | |
149 | ||
150 | <p><br /></p><hr class='online-navigation' /> | |
151 | <div class='online-navigation'> | |
152 | <!--Table of Child-Links--> | |
153 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
154 | ||
155 | <UL CLASS="ChildLinks"> | |
156 | <LI><A href="bisect-example.html">5.11.1 Examples</a> | |
157 | </ul> | |
158 | <!--End of Table of Child-Links--> | |
159 | </div> | |
160 | ||
161 | <DIV CLASS="navigation"> | |
162 | <div class='online-navigation'> | |
163 | <p></p><hr /> | |
164 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
165 | <tr> | |
166 | <td class='online-navigation'><a rel="prev" title="5.10 whrandom " | |
167 | href="module-whrandom.html"><img src='../icons/previous.png' | |
168 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
169 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" | |
170 | href="misc.html"><img src='../icons/up.png' | |
171 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
172 | <td class='online-navigation'><a rel="next" title="5.11.1 Examples" | |
173 | href="bisect-example.html"><img src='../icons/next.png' | |
174 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
175 | <td align="center" width="100%">Python Library Reference</td> | |
176 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
177 | href="contents.html"><img src='../icons/contents.png' | |
178 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
179 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
180 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
181 | <td class='online-navigation'><a rel="index" title="Index" | |
182 | href="genindex.html"><img src='../icons/index.png' | |
183 | border='0' height='32' alt='Index' width='32' /></A></td> | |
184 | </tr></table> | |
185 | <div class='online-navigation'> | |
186 | <b class="navlabel">Previous:</b> | |
187 | <a class="sectref" rel="prev" href="module-whrandom.html">5.10 whrandom </A> | |
188 | <b class="navlabel">Up:</b> | |
189 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> | |
190 | <b class="navlabel">Next:</b> | |
191 | <a class="sectref" rel="next" href="bisect-example.html">5.11.1 Examples</A> | |
192 | </div> | |
193 | </div> | |
194 | <hr /> | |
195 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
196 | </DIV> | |
197 | <!--End of Navigation Panel--> | |
198 | <ADDRESS> | |
199 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
200 | </ADDRESS> | |
201 | </BODY> | |
202 | </HTML> |