I found this post, I tried to implement top-n heaps with a fixed size, here is a solution I can offer:
from heapq import heapify, heappush, heappushpop class MaxHeap(): def __init__(self, top_n): self.h = [] self.length = top_n heapify( self.h) def add(self, element): if len(self.h) < self.length: heappush(self.h, element) else: heappushpop(self.h, element) def getTop(self): return sorted(self.h, reverse=True)
source share