Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61002

Re: extracting a heapq in a for loop - there must be more elegant solution

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'memory.': 0.07; 'executed': 0.09; 'extracted': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'heap': 0.16; 'heapq': 0.16; 'iterating': 0.16; 'loop.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sorted()': 0.16; 'elements': 0.16; 'pushed': 0.16; 'wrote:': 0.18; 'bit': 0.19; '>>>': 0.22; 'import': 0.22; 'header :User-Agent:1': 0.23; 'adds': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'dec': 0.30; "skip:' 10": 0.31; '+0100,': 0.31; 'lessons': 0.31; 'file': 0.32; 'thanks!': 0.32; "i'd": 0.34; 'could': 0.34; 'except': 0.35; 'but': 0.35; 'yield': 0.36; 'doing': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'application': 0.37; 'so,': 0.37; 'too': 0.37; 'list': 0.37; 'list.': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'save': 0.62; 'complete': 0.62; 'real': 0.63; 'subject:more': 0.64; 'more': 0.64; 'here': 0.66; 'subject:there': 0.68; 'below.': 0.71; 'end.': 0.84; 'otten': 0.84; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: extracting a heapq in a for loop - there must be more elegant solution
Date Wed, 04 Dec 2013 10:50:52 +0100
Organization None
References <bg60hjF83eoU1@mid.dfncis.de> <mailman.3509.1386074347.18130.python-list@python.org> <bg8bchFn3pfU2@mid.dfncis.de>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084b2f9.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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.3550.1386150658.18130.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386150658 news.xs4all.nl 2830 [2001:888:2000:d::a6]:42089
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61002

Show key headers only | View raw


Helmut Jarausch wrote:

> On Tue, 03 Dec 2013 13:38:58 +0100, Peter Otten wrote:
> 
>> Helmut Jarausch wrote:
>> 
>>> Hi,
>>> 
>>> 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 know I could use a while loop but I don't like it.
>>> 
>>> Many thanks for some lessons in Python.
>>> 
>>> Here is my clumsy solution
>>> 
>>> from heapq import heappush, heappop
>>> # heappop raises IndexError if heap is empty
>>> 
>>> H=[]
>>> for N in 'H','C','W','I' :
>>>   heappush(H,N)
>> 
>> H = ["H", "C", "W", "I"]
>> heapq.heapify(H)
>> 
>> But see below.
>> 
>>> # how to avoid / simplify the following function
>>> 
>>> def in_sequence(H) :
>>>   try :
>>>     while True :
>>>       N= heappop(H)
>>>       yield N
>>>   except IndexError :
>>>     raise StopIteration
>>> 
>>> # and here the application:
>>> 
>>> for N in in_sequence(H) :
>>>   print(N)
>> 
>> If you are iterating over the complete heap I see no advantage over a
>> sorted list. So
>> 
>> for N in sorted(H):
>>     print(N)
>> 
>> If H is huge use H.sort() instead of sorted() to save memory.
>> If you need only a few items use heapq.nsmallest().
> 
> 
> Many thanks!
> In my real application the data which is pushed onto the heap will be
> extracted from a small file which is executed several thousands times. So,
> I thought, I could keep the CPU a bit busy while the OS is doing file I/O.
> Of course, I could have appended all these strings to a list which is
> sorted at the end.

In that case have a look at bisect.insort() which adds an item to a list 
while keeping it sorted.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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