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: 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 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: User-Agent: Mutt/1.5.21 (2010-09-15) References: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 03Dec2013 12:18, Helmut Jarausch 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 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