Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61000
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail |
|---|---|
| From | Helmut Jarausch <jarausch@igpm.rwth-aachen.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: extracting a heapq in a for loop - there must be more elegant solution |
| Date | 4 Dec 2013 09:44:49 GMT |
| Lines | 45 |
| Message-ID | <bg8bshFn3pfU6@mid.dfncis.de> (permalink) |
| References | <bg60hjF83eoU1@mid.dfncis.de> <mailman.3528.1386105197.18130.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.dfncis.de xxY4HMvABAPAzEbPICCwnQYA16PeR7u6Lq0kfmMNYIDZYQu3lPQG9yNbj4 |
| Cancel-Lock | sha1:+kZV1Y5dQVWlQH0gNMkmKa/VslA= |
| User-Agent | Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) |
| Xref | csiph.com comp.lang.python:61000 |
Show key headers only | View raw
On Wed, 04 Dec 2013 08:13:03 +1100, Cameron Simpson wrote: > On 03Dec2013 12:18, Helmut Jarausch <jarausch@igpm.rwth-aachen.de> wrote: >> I'd like to extracted elements from a heapq in a for loop. >> I feel my solution below is much too complicated. >> How to do it more elegantly? > > I can't believe nobody has mentioned PriorityQueue. > > A PriorityQueue (from the queue module in python 3 and the Queue > module in python 2) is essentially just a regular Queue using a > heapq for the storage. > > Example (untested): > > from Queue import PriorityQueue > > PQ = PriorityQueue() > for item in [1,2,3,7,6,5,9,4]: > PQ.put(item) > > while not PQ.empty(): > item = PQ.get() > ... do stuff with item ... > > I iterate over Queues so often that I have a personal class called > a QueueIterator which is a wrapper for a Queue or PriorityQueue > which is iterable, and an IterablePriorityQueue factory function. > Example: > > from cs.queues import IterablePriorityQueue > > IPQ = IterablePriorityQueue() > for item in [1,2,3,7,6,5,9,4]: > IPQ.put(item) > > for item in IPQ: > ... do stuff with item ... > > Cheers, Many thanks! I think you QueueIterator would be a nice addition to Python's library. Helmut
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
extracting a heapq in a for loop - there must be more elegant solution Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-12-03 12:18 +0000
Re: extracting a heapq in a for loop - there must be more elegant solution Peter Otten <__peter__@web.de> - 2013-12-03 13:38 +0100
Re: extracting a heapq in a for loop - there must be more elegant solution Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-12-04 09:36 +0000
Re: extracting a heapq in a for loop - there must be more elegant solution Peter Otten <__peter__@web.de> - 2013-12-04 10:50 +0100
Re: extracting a heapq in a for loop - there must be more elegant solution rusi <rustompmody@gmail.com> - 2013-12-03 04:40 -0800
Re: extracting a heapq in a for loop - there must be more elegant solution Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-12-04 09:37 +0000
Re: extracting a heapq in a for loop - there must be more elegant solution Duncan Booth <duncan.booth@invalid.invalid> - 2013-12-03 13:06 +0000
Re: extracting a heapq in a for loop - there must be more elegant solution Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-12-04 09:39 +0000
Re: extracting a heapq in a for loop - there must be more elegant solution Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-12-03 15:56 +0200
Re: extracting a heapq in a for loop - there must be more elegant solution Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-12-04 09:41 +0000
Re: extracting a heapq in a for loop - there must be more elegant solution Cameron Simpson <cs@zip.com.au> - 2013-12-04 08:13 +1100
Re: extracting a heapq in a for loop - there must be more elegant solution Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2013-12-04 09:44 +0000
Re: extracting a heapq in a for loop - there must be more elegant solution Cameron Simpson <cs@zip.com.au> - 2013-12-05 13:29 +1100
Re: extracting a heapq in a for loop - there must be more elegant solution Ian Kelly <ian.g.kelly@gmail.com> - 2013-12-03 14:43 -0700
Re: extracting a heapq in a for loop - there must be more elegant solution Ned Batchelder <ned@nedbatchelder.com> - 2013-12-03 17:32 -0500
csiph-web