python使用heapq实现小顶堆(TopK大)/大顶堆(BtmK小)

参考链接

https://www.coder4.com/archives/3844

求一个数列前K大数的问题经常会遇到,在程序中一般用小顶堆可以解决,下面的代码是使用python的heapq实现的小顶堆示例代码:

    # !/usr/bin/env python
    # -*- coding:gbk -*-

    import sys
    import heapq

    class TopKHeap(object):
        def __init__(self, k):
            self.k = k
            self.data = []

        def push(self, elem):
            if len(self.data) < self.k:
                heapq.heappush(self.data, elem)
            else:
                topk_small = self.data[0]
                if elem > topk_small:
                    heapq.heapreplace(self.data, elem)
        def topk(self):
            return [x for x in reversed([heapq.heappop(self.data) for x in xrange(len(self.data))])]


    def main():
        list_num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        th = TopKHeap(5)

        for i in list_num:
            th.push(i)

        print th.topk()

    if __name__ == "__main__":
        main()

python的heapq在实现的时候,没有像STL或者Java可以传入比较函数,具体的原因可以参考参考文档给出的链接。

因此有些人想出了比较trick的思路。一句话概括如下:

push(e)改为push(-e),pop(e)为-pop(e),也就是说存入和取出的数都是相反数,其他逻辑和TopK相同。(点赞)

实现用户自定义的比较函数,允许elem是一个tuple,按照tuple的第一个元素进行比较,所以可以把tuple的第一个元素作为我们的比较的key。

英文原话:

The heapq documentation suggests that heap elements could be tuples in which the first element is the priority and defines the sort order.

    import heapq
    class MyHeap(object):
       def __init__(self, initial=None, key=lambda x:x):
           self.key = key
           if initial:
               self._data = [(key(item), item) for item in initial]
               heapq.heapify(self._data)
           else:
               self._data = []

       def push(self, item):
           heapq.heappush(self._data, (self.key(item), item))

       def pop(self):
           return heapq.heappop(self._data)[1]
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值