Mercurial > cpython-lru_cache-weakref
diff Doc/lib/libbisect.tex @ 23996:703e63ae0f01 legacy-trunk
add /F's PriorityQueue example
| author | Skip Montanaro <skip@pobox.com> |
|---|---|
| date | Wed, 26 Jun 2002 05:07:28 +0000 |
| parents | eeb2e77c5bad |
| children | 63fa60f2d61c |
line wrap: on
line diff
--- a/Doc/lib/libbisect.tex Wed Jun 26 02:32:09 2002 +0000 +++ b/Doc/lib/libbisect.tex Wed Jun 26 05:07:28 2002 +0000 @@ -61,7 +61,7 @@ \end{funcdesc} -\subsection{Example} +\subsection{Examples} \nodename{bisect-example} The \function{bisect()} function is generally useful for categorizing @@ -81,3 +81,21 @@ >>> map(grade, [33, 99, 77, 44, 12, 88]) ['E', 'A', 'B', 'D', 'F', 'A'] \end{verbatim} + +The bisect module can be used with the Queue module to implement a priority +queue (example courtesy of Fredrik Lundh): \index{Priority Queue} + +\begin{verbatim} +import Queue, bisect + +class PriorityQueue(Queue.Queue): + def _put(self, item): + bisect.insort(self.queue, item) + +# usage +queue = PriorityQueue(0) +queue.put((2, "second")) +queue.put((1, "first")) +queue.put((3, "third")) +priority, value = queue.get() +\end{verbatim}
