Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- h = []
- heapq.heappush(h,(10, 1200))
- heapq.heappush(h,(20, 31))
- heapq.heappush(h,(5, 1))
- if len(h) < capacity:
- heapq.heappush(h, thing)
- else:
- # Equivalent to a push, then a pop, but faster
- spilled_value = heapq.heappushpop(h, thing)
- do_whatever_with(spilled_value)
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement