Python heapq Usage Details and instance code, pythonheapq
Python heapq
Python has a built-in module. The heapq standard encapsulates the minimum heap algorithm implementation. Let's take a look at two good applications.
Small top heap (top K)
The requirement is as follows: a sequence with a fixed length is used to obtain top K big data.
import heapqimport randomclass 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))])]if __name__ == "__main__": print "Hello" list_rand = random.sample(xrange(1000000), 100) th = TopkHeap(3) for i in list_rand: th.Push(i) print th.TopK() print sorted(list_rand, reverse=True)[0:3]
Big Top heap (small BtmK)
This demand has become more difficult: give N long sequences and find small BtmK elements, that is, use a large top heap.
The core idea of Algorithm Implementation is to change push (e) to push (-e) and pop (e) to-pop (e ).
class BtmkHeap(object): def __init__(self, k): self.k = k self.data = [] def Push(self, elem): # Reverse elem to convert to max-heap elem = -elem # Using heap algorighem 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 BtmK(self): return sorted([-x for x in self.data])
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!