Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail From: Helmut Jarausch Newsgroups: comp.lang.python Subject: Re: extracting a heapq in a for loop - there must be more elegant solution Date: 4 Dec 2013 09:36:17 GMT Lines: 57 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.dfncis.de 1VNJoyRNmT+sgHU4Uxw5Rg7r5VSyv3Ppat2OXW3OX4tv4xEemDIZAJZlj0 Cancel-Lock: sha1:eikqCnJhddDiXY+KZVcK2svABP8= User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Xref: csiph.com comp.lang.python:60996 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.