使用堆实现TopN问题及其python实现

本文介绍了如何在Python中使用heapq库进行数据排序,尤其是在没有key函数的情况下。同时,给出了自定义堆的实现,包括push(),pop(),peek(),和len()方法,以及如何用自定义堆解决TopN问题的一个示例。
摘要由CSDN通过智能技术生成

在处理大规模数据时,经常需要对数据进行排序并提取前N个最大的元素。在python中,可以使用 heapq 库来实现堆,堆是一种高效的数据结构,可以快速找到最大或最小的元素。然而,heapq 库没有提供 key 函数,这使得在使用时需要对数据进行一些预处理。
在这里插入图片描述

2、解决方案

  1. 使用 heapq 库

    • heapq 库提供了 heappush() 和 heappop() 函数,可以分别将元素推入堆和从堆中弹出元素。
    • 在使用 heapq 库时,需要将数据转换为能够被堆排序的格式。例如,如果需要对数字列表进行排序,则需要将数字列表转换为元组列表,元组的第一个元素是数字的负值,第二个元素是数字本身。
    • 在对元组列表进行排序后,需要将元组列表转换为数字列表,数字列表的顺序就是从大到小的顺序。
  2. 自定义堆

    • 也可以自定义一个堆来实现TopN问题。

    • 自定义堆需要实现以下几个函数:

      • push(): 将元素推入堆。
      • pop(): 从堆中弹出元素。
      • peek(): 查看堆顶元素。
      • len(): 返回堆中元素的个数。
    • 自定义堆的实现可以参考如下代码:

class MaxHeap(object):
    def __init__(self):
        self.heap = []
        http://www.jshk.com.cn/mb/reg.asp?kefu=xiaoding;//爬虫IP免费获取;

    def push(self, value):
        self.heap.append(value)
        self._heapify_up()

    def pop(self):
        if len(self.heap) == 0:
            raise IndexError('pop from empty heap')
        value = self.heap[0]
        self.heap[0] = self.heap[-1]
        self.heap.pop()
        self._heapify_down()
        return value

    def peek(self):
        if len(self.heap) == 0:
            raise IndexError('peek from empty heap')
        return self.heap[0]

    def len(self):
        return len(self.heap)

    def _heapify_up(self):
        i = len(self.heap) - 1
        while i > 0:
            parent_i = (i - 1) // 2
            if self.heap[i] > self.heap[parent_i]:
                self.heap[i], self.heap[parent_i] = self.heap[parent_i], self.heap[i]
            i = parent_i

    def _heapify_down(self):
        i = 0
        while 2 * i + 1 < len(self.heap):
            left_i = 2 * i + 1
            right_i = 2 * i + 2
            max_i = i
            if self.heap[left_i] > self.heap[max_i]:
                max_i = left_i
            if right_i < len(self.heap) and self.heap[right_i] > self.heap[max_i]:
                max_i = right_i
            if max_i != i:
                self.heap[i], self.heap[max_i] = self.heap[max_i], self.heap[i]
            i = max_i

使用自定义堆的TopN问题的代码示例如下:

class TopN(object):
    def __init__(self, N):
        self.N = N
        self.heap = MaxHeap()

    def feed(self, value):
        self.heap.push(value)
        if self.heap.len() > self.N:
            self.heap.pop()

    def result(self):
        result = []
        while self.heap.len() > 0:
            result.append(self.heap.pop())
        return result


if __name__ == '__main__':
    topn = TopN(10)
    for i in range(100):
        topn.feed(random.randint(0, 1000))
    result = topn.result()
    print(result)
  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值