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


Groups > comp.lang.python > #89458

Re: heapq - why no key= arg?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2a.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python3': 0.07; 'suppose': 0.07; '"__main__":': 0.09; '[1,': 0.09; '__name__': 0.09; 'calculating': 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; '"\\n")': 0.16; 'customizing': 0.16; 'heapq': 0.16; 'itertools': 0.16; 'key):': 0.16; 'key=': 0.16; 'once.': 0.16; 'operation.': 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; 'subject:key': 0.16; 'value))': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'written': 0.21; 'import': 0.22; 'header:User-Agent:1': 0.23; 'removed.': 0.24; 'header:X-Complaints-To:1': 0.27; 'operations,': 0.30; "i'm": 0.30; 'comparison': 0.31; 'index,': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'operations': 0.35; 'subject:?': 0.36; 'too': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'such': 0.63; 'skip:n 10': 0.64; 'provide': 0.64; 'more': 0.64; 'lazy': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: heapq - why no key= arg?
Date Mon, 27 Apr 2015 16:24:32 +0200
Organization None
References <mhlf3c$lba$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd94db.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.44.1430144702.3680.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430144702 news.xs4all.nl 2909 [2001:888:2000:d::a6]:54752
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89458

Show key headers only | View raw


Neal Becker wrote:

> Looking at heapq, I see only nlargest/nsmallest provide a key= arg.  What
> about other operations, such as heapify?  Am I not understanding
> something? 

nlargest/nsmallest() use the decorate-sort-undecorate pattern to avoid 
calculating the key more than once. For the other operations you'd have to 
calculate key(item) repeatedly for every item involved in the specific 
operation.

> I suppose for other ops, such as heapify, I can only customize
> comparison by customizing the object comparison operators?

I'm sure someons's written a Heapq class that automatically decorates when 
an item is added and undecorates when one is looked up or removed. As I'm 
too lazy to look it up here's a sketch:

$ cat myheapq.py
import heapq
from itertools import count


class Heapq:
    def __init__(self, values, key):
        self.counter = count()
        self.key = key
        self.items = [(key(value), index, value)
                      for index, value in zip(self.counter, values)]
        heapq.heapify(self.items)

    def push(self, value):
        heapq.heappush(
            self.items,
            (self.key(value), next(self.counter), value))

    def pop(self):
        return heapq.heappop(self.items)[-1]

    def __str__(self):
        return str([item[-1] for item in self.items])

    def __bool__(self):
        return not not self.items

if __name__ == "__main__":
    h = Heapq([1, -2, -3, 4], key=abs)
    print(h)
    print(h.pop())
    print(h)
    h.push(-5)
    print(h)

    while h:
        print(h.pop(), end=", " if h else "\n")
$ python3 myheapq.py 
[1, -2, -3, 4]
1
[-2, 4, -3]
[-2, 4, -3, -5]
-2, -3, 4, -5

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


Thread

Re: heapq - why no key= arg? Peter Otten <__peter__@web.de> - 2015-04-27 16:24 +0200

csiph-web