Questions tagged [heapq]

A Python module that implements the heap queue algorithm. Use only when the question is specific to this heap implementation, as opposed to heap mechanics in general.

Module documentation: heapq

Alternative, using a different interface: queue.PriorityQueue

See also the language-agnostic tag.

90 questions
0
votes
1 answer

Python, heapq: difference between heappushpop() and peeking/popping

Is there a significant difference in functionality and/or efficiency between using heappushpop and peeking at the heap first then deciding whether to pop (using heapreplace)? E.g. from heapq import * a = [5, 18, 9, 14, 22] heapify(a) # a = [5, 14,…
g999
  • 173
  • 1
  • 9
0
votes
1 answer

numba jit mode memory leak issue?

I am currently facing a weird memory leak problem when having jit decorator on a function which uses heapify takings results from another jitted function which return results in yield style here is the idea of the design to indicate the…
Axd
  • 45
  • 2
0
votes
3 answers

Why do I get TypeError, when trying to find the max element of a heap with function 'nlargest'?

I was trying to find the maximum element of a heap and found a function heapq.nlargest to use. Then got this error at the line commented below: TypeError at line 10: 'NoneType' object is not iterable. So here is the code: from heapq import * from…
0
votes
1 answer

Unorderable type error while pushing into heap

I have a list of heads of N Linked Lists which I am trying to merge based on their value using heaps. Since heapq works with first value in tuples for sorting in heap I wrote the following code: def mergeKLists(self, A): # A contains list of Linked…
Abhishek Agarwal
  • 1,190
  • 2
  • 19
  • 38
0
votes
1 answer

Why does the Python heapq _siftup(...) call _siftdown(...) at the end?

The code for_siftup at github - python/cpython/Lib/heapq.py has a final call to _siftdown: def _siftup(heap, pos): endpos = len(heap) startpos = pos newitem = heap[pos] # Bubble up the smaller child until hitting a leaf. …
aksg87
  • 63
  • 1
  • 9
0
votes
1 answer

Interviewbit - Merge k sorted linked lists: heappop returns max element instead of min

I'm solving the Interviewbit code challenge Merge K Sorted Lists: Merge k sorted linked lists and return it as one sorted list. Example : 1 -> 10 -> 20 4 -> 11 -> 13 3 -> 8 -> 9 will result in 1 -> 3 -> 4 -> 8 -> 9 -> 10 -> 11 -> 13 -> 20 The…
K2G
  • 27
  • 1
  • 9
0
votes
2 answers

Sorting (or partially sorting) a list of objects based on a specific attribute

Problem I have a list of objects. Each object has two attributes: "score" and "coordinates". I need to find the largest N objects of the list based on the score attribute. The main problem I'm having is sorting the objects using only the score…
spengler
  • 3
  • 6
0
votes
0 answers

Understand dict.get without braces in Python

For the question https://leetcode.com/problems/top-k-frequent-elements to find top k frequent elements I understand we can use heapq.nlargest() method but for that the key used is just dict.get without braces -…
0
votes
1 answer

Weird Python heap: heappop() output at the first time

the first heappop() is not the min of array, while it's the first index. After that, heappop() works I thought maybe it's the automatic heapify() after heappop()? check the documents but find nothing >>> a =…
Cici Hou
  • 3
  • 1
0
votes
3 answers

Merging k sorted lists using heapq module in python3

Problem:- merge k sorted lists. I want to solve this problem using min heap which can be implemented with heapq module in python. Below is the sample code of the function... heapq.heappush(listwithoutNone,(node.val, node)) But the problem…
0
votes
1 answer

Why PyCharm debugger crashes with "ImportError: cannot import name 'heappush'"

I have a project in PyCharm that its debugger began crashing after I added a few lines of code. I deleted all the code and left a simple print in the file. The code is located in the folder: "heapq\test_heapq.py" as shown in screenshot 1. The error…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
0
votes
0 answers

Listing the shortest path available using heapq for dijkstra shortest path algorithm

I was given a task where I need to apply Dijkstra's Shortest Path Algorithm in my python code. Let say we have a few cities involved. I need to find the one that has the shortest route. In summary, the task is to: Find the shortest route. List all…
0
votes
0 answers

heap time complexity python

import heapq def getMaxUnit(num,boxes,UnitSize,UnitSize,unitPerBox, truckSize): if truckSize == 0 or num == 0: return 0 h = [] for i in range(num): h.append((-1*unitPerBox[i],boxes[i])) heapq.heapify(h) …
0
votes
2 answers

Applying Dijkstra with heapq: 'generator' object is not subscriptable

As the title suggests i'm trying to apply dijkstra algorithm with a Heapq (following bogotobogo.com on dijksta in python with slight modifications - long link;could't type), and i'm getting TypeError: 'generator' object is not subscriptable with the…
Mohab Khaled
  • 167
  • 1
  • 8
0
votes
1 answer

How to use heapq module

I don't understand how I can properly use heapq module. I realized that without transforming my list to a heap (without using heapify) I can still use other functions which require a heap as input (heappop, heappush..). So when do I need to use…
kneazle
  • 335
  • 4
  • 14