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


Groups > comp.lang.python > #60998

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

From Helmut Jarausch <jarausch@igpm.rwth-aachen.de>
Newsgroups comp.lang.python
Subject Re: extracting a heapq in a for loop - there must be more elegant solution
Date 2013-12-04 09:39 +0000
Message-ID <bg8bi9Fn3pfU4@mid.dfncis.de> (permalink)
References <bg60hjF83eoU1@mid.dfncis.de> <XnsA28B844A38AD0duncanbooth@127.0.0.1>

Show all headers | View raw


On Tue, 03 Dec 2013 13:06:05 +0000, Duncan Booth wrote:

> Helmut Jarausch <jarausch@igpm.rwth-aachen.de> 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)
>> 
>> # 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 all you want to do is pull all of the elements out of the heap in 
> order, you would probably be better off just doing:
> 
> for N in sorted(H):
>     print(N)
> 
> Heaps are mostly useful if you want only some of the elements, or if you 
> are continually producing more elements while also processing the 
> smallest ones.
> 
> However, if you really wnt to do this:
> 
>     for N in iter(lambda: heappop(H) if H else None, None):
>         print(N)
> 
> will work so long as H cannot contain None. If it can just replace both 
> occurences of None with some other sentinel:
> 
>     sentinel = object()
>     for N in iter(lambda: heappop(H) if H else sentinel, sentinel):
>         print(N)
> 
> 
> Alternatively your 'in_sequence' function would look better without the 
> exception handling:
> 
> def in_sequence(H) :
>     while H:
>         yield heappop(H)

Many thanks!
And as noted in another reply, I try to overlap CPU time with file I/O since
I have to open / read / close a file for each line that gets pushed onto the heap.
Helmut

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