Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60961
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'essentially': 0.04; 'extracted': 0.09; 'iterate': 0.09; 'storage.': 0.09; 'wrapper': 0.09; 'python': 0.11; 'factory': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'heapq': 0.16; 'henry': 0.16; 'iterable,': 0.16; 'loop.': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'reinvent': 0.16; 'simpson': 0.16; 'subject: \n ': 0.16; 'url:cskk': 0.16; 'url:ezoshosting': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'module': 0.19; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'cheers,': 0.24; 'header:In-Reply- To:1': 0.27; 'unix': 0.29; 'class': 0.32; 'stuff': 0.32; 'regular': 0.32; "i'd": 0.34; "can't": 0.35; 'received:com.au': 0.36; 'charset:us-ascii': 0.36; 'too': 0.37; 'received:211': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; '(from': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'subject: ': 0.61; 'mentioned': 0.61; 'content-disposition:inline': 0.62; 'personal': 0.63; 'subject:more': 0.64; 'more': 0.64; 'believe': 0.68; 'nobody': 0.68; 'subject:there': 0.68; 'toronto': 0.91 |
| Date | Wed, 4 Dec 2013 08:13:03 +1100 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | python-list@python.org |
| Subject | Re: extracting a heapq in a for loop - there must be more elegant solution |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Disposition | inline |
| In-Reply-To | <bg60hjF83eoU1@mid.dfncis.de> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| References | <bg60hjF83eoU1@mid.dfncis.de> |
| X-Optus-CM-Score | 0 |
| X-Optus-CM-Analysis | v=2.1 cv=YYGEuWhf c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=jRJTwREybbQA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=8AHkEIZyAAAA:8 a=sJoo9Cljmp4A:10 a=lyG38BaPAAAA:8 a=4Jym8Q_jdICGQ6ZnDEQA:9 a=CjuIK1q_8ugA:10 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3528.1386105197.18130.python-list@python.org> (permalink) |
| Lines | 43 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1386105197 news.xs4all.nl 2877 [2001:888:2000:d::a6]:55302 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60961 |
Show key headers only | View raw
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,
--
Cameron Simpson <cs@zip.com.au> http://www.cskk.ezoshosting.com/cs/
Those who do not understand Unix are condemned to reinvent it, poorly.
- Henry Spencer @ U of Toronto Zoology, henry@zoo.toronto.edu
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