Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'extracted': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'mostly': 0.14; 'heapq': 0.16; 'loop.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'simpson': 0.16; 'subject: \n ': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'bit': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; "haven't": 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'dec': 0.30; "i'm": 0.30; "i'd": 0.34; "can't": 0.35; 'but': 0.35; 'thanks': 0.36; 'too': 0.37; 'same.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'ian': 0.60; 'mentioned': 0.61; 'subject:more': 0.64; 'more': 0.64; 'believe': 0.68; 'nobody': 0.68; 'subject:there': 0.68; 'fact,': 0.69; 'imagine': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: extracting a heapq in a for loop - there must be more elegant solution Date: Tue, 03 Dec 2013 17:32:39 -0500 References: <20131203211303.GA83257@cskk.homeip.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 18.189.126.187 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 In-Reply-To: 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386109973 news.xs4all.nl 2863 [2001:888:2000:d::a6]:46820 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60967 On 12/3/13 4:43 PM, 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. > In fact, if you look at the implementation of PriorityQueue, it uses a heapq internally, so yes, other than the locking, the speed will be the same. --Ned.