Python heapq Usage Details and instance code, pythonheapq

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.