Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'insertion': 0.09; 'iterate': 0.09; 'cc:addr:python-list': 0.11; '(either': 0.16; '-tkc': 0.16; 'duplicates': 0.16; 'heap': 0.16; 'heapq': 0.16; 'nsmallest': 0.16; 'to:addr:python.list': 0.16; 'to:addr:tim.thechases.com': 0.16; 'to:name:tim chase': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'sort': 0.25; "i've": 0.25; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; 'tim': 0.29; 'chase': 0.31; 'maintains': 0.31; 'once,': 0.31; 'url:python': 0.33; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'list': 0.37; 'list.': 0.37; 'url:library': 0.38; 'list,': 0.38; 'pm,': 0.38; 'little': 0.38; 'according': 0.40; 'skip:n 10': 0.64; 'different': 0.65; 'results': 0.69; 'walk': 0.74; 'potentially': 0.81; 'delaying': 0.84; 'subject:skip:n 10': 0.84; 'items,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=XHP7ZKoFDGcQEz1ofu9AnADSFbmOro088GE2ZpKZG1M=; b=V2eOxwmnkVTdsSmGtI7HNVALAwc9GgxvDbh0txPO4GaBgWp3JfwEOHbkqB/JVHuaf8 iLDOIz2LILs2g5iTgnWlGBsojOyhM2KXoYoymwuBt7IrHJPXQQePuU6k7caO8+/6eAjq 109GecAN9EOET2HW0d395MjGgEdl2n38jpJ+TsYRe4FD+TmbwwjSJ+SumOEhHMUNzPoX uXDDsrUwoIvo1Z0HHcrz86byCnd3FWRbNWHRR+TisCkaqwm2cFP6Mad6RaaxIibQ7yl1 dSmalQF+vWHX3FAWaU0JIeZp35rV0fMO7bcrCstycEZuKk56XTJpVnwtJ9hHco8wvk6y hnIw== X-Received: by 10.49.29.230 with SMTP id n6mr12218212qeh.20.1380323283709; Fri, 27 Sep 2013 16:08:03 -0700 (PDT) Sender: Ned Batchelder Date: Fri, 27 Sep 2013 19:08:02 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: Tim Chase Subject: Re: walking a heapq nondestructively without duplicating? References: <20130927172214.1d9be803@bigbox.christie.dr> In-Reply-To: <20130927172214.1d9be803@bigbox.christie.dr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380323292 news.xs4all.nl 15958 [2001:888:2000:d::a6]:42861 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54917 On 9/27/13 6:22 PM, Tim Chase wrote: > I've got a large heapq'ified list and want to walk it in-order > without altering it. I get the "unsorted" heap'ish results if I just > do > > from heapq import heappush, heappop, nlargest, nsmallest > my_heap = [] > for thing in lots_of_items(): > heappush(thing) > for item in my_heap: > ... > > To get them in-order, I can do something like > > while my_heap: > item = heappop(my_heap) > do_something(item) > > to iterate over the items in order, but that destroys the original > heap. I can also do > > for item in nlargest(len(my_heap), my_heap): # or nsmallest() > do_something(item) > > but this duplicates a potentially large list according to my > reading of the description for nlargest/nsmallest[1]. Is there a > handy way to non-destructively walk the heap (either in-order or > reversed) without duplicating its contents? If you add all your items at once, and then you want to walk over all the items, then don't use a heap. Just put all your items in a list, and then sort it. The advantage of a heap is that you can add items to it with little effort, delaying some of the work until when you need to get the items out. It maintains a partially-sorted list that's good for insertion and popping. You have different needs. Use a sorted list. --Ned. > -tkc > > [1] http://docs.python.org/2/library/heapq.html#heapq.nlargest > > > > > > >