Heap queue (or heapq) in Python:

The heap data structure is there mainly for representing a priority queue. In Python heapq module is available for this purpose. The main property of this data function is that each time the smallest of heap element is popped(min heap). When the elements are pushed or popped a heap structure is maintained. The element heap[0] returns the smallest element every time in python for data science.

Operations on heap:
  1. heapify(iterable): It is a function which is there for converting the iterable into a heap data structure, in heap order.
  2. heappush(heap, ele): It is a function there for inserting the element mention in the arguments into heap. There is adjustment of order so, as heap structure is maintain.
  3. heappop(heap): It is a function that is there for removing and returning the smallest element from heap. There is adjustment of order so as heap structure is maintain.Heap queue (or heapq) in Python for Data Science - PST Analytics
  4. heappushpop(heap, ele): When we use this function it combines the functioning of both push and pop operations in one statement which increases efficiency. The heap order is maintain after this operation.
  5. Heapreplace(heap, ele): It is a function that inserts and pops the elements in one statement. But this is different from the above functions. The element is first popped, and after that, it is pushed so, the value which is larger than pushed value can be returned.Heap queue (or heapq) in Python for Data Science - PST Analytics
  6. nlargest(k, iterable, key = fun): It is a function that is there for returning the k largest elements from the iterable which is specific and satisfying the key if it is mention.
  7. Nsmallest(k, iterable, key = fun): It is a function which is there for returning the k smallest elements from iterable which is specific and satisfying the key if it is mention.

 

To learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.