def push(self, elem):
'''Push new elem to heap
Args:
elem ?the elem to add
Returns:
if the elem have added to queue
'''
if len(self.data) < self.k:
heapq.heappush(self.data, elem)
return True
else:
topk_small = self.data[0]
if elem > topk_small:
heapq.heapreplace(self.data, elem)
return True
return False
评论列表
文章目录