Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'extracted': 0.09; 'mostly': 0.14; '"are': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'heap': 0.16; 'heapq': 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; 'simpson': 0.16; 'stuff,': 0.16; 'subject: \n ': 0.16; 'elements': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'bit': 0.19; 'header:User-Agent:1': 0.23; 'cheers,': 0.24; "haven't": 0.24; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'dec': 0.30; "i'm": 0.30; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'really': 0.36; "he's": 0.36; 'received:com.au': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'too': 0.37; 'list.': 0.37; 'received:211': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'heard': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'ian': 0.60; 'mentioned': 0.61; 'content-disposition:inline': 0.62; 'subject:more': 0.64; 'more': 0.64; 'believe': 0.68; 'nobody': 0.68; 'subject:there': 0.68; 'imagine': 0.93; '2013': 0.98 Date: Wed, 4 Dec 2013 10:27:25 +1100 From: Cameron Simpson To: Python 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=DstvpgP+ c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=MqjIacvGb54A:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=sJoo9Cljmp4A:10 a=pGLkceISAAAA:8 a=pIwDaDRV5l-GWcQjaLgA:9 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A: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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386113252 news.xs4all.nl 2926 [2001:888:2000:d::a6]:35208 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60971 On 03Dec2013 14:43, Ian Kelly wrote: > On Tue, Dec 3, 2013 at 2:13 PM, Cameron Simpson wrote: > > 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. > > As far as I'm aware, the only advantage of PriorityQueue over heapq is > that the former is thread-safe, which does not appear to be relevant > here. I haven't tested it for speed, but I imagine it would be a fair > bit slower, mostly thanks to the locking it needs to do. I could claim I do a lot of multithreaded stuff, which is true. But really I just prefer the put/get abstraction. A heapq is very overtly a heap manipulator applied to an arbitrary list. A Queue is more self contained, for all that it is the same thing happening inside. Cheers, -- Cameron Simpson "Are we alpinists, or are we tourists" followed by "tourists! tourists!" - Kobus Barnard in rec.climbing, on things he's heard firsthand