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

Why does `heapq.heapify` implementation work?

I was trying to understand how to convert a list to a min-heap. I did my own simple recursive implementation which seems to work, but I was curious to understand how heapq.heapify works. Thinking of the representation of an array as a heap, the…
Deathcrush
  • 77
  • 7
0
votes
1 answer

How can heapq's push operation be O(log n) time if the underlying data structure is a list?

Doesn't a list require O(n) time to increase its size? How, then, could heap.heappush be O(log n)?
Lay González
  • 2,901
  • 21
  • 41
0
votes
0 answers

How do I use heapq to push a new element to my max heap?

>>> import heapq as h >>> l = [1,3,6,2,9] >>> dir(h) ['__about__', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_heapify_max', '_heappop_max', '_heapreplace_max', '_siftdown',…
0
votes
0 answers

Python Priority Queue with objects heappush does not support '>' between objects

In my attempt to implement a simple version of A* Search, I am trying to enqueue the following in a priority queue: Priority: sum of heuristic value and cost from source to current element Queue Node: Object of class queue node with data members…
Shahzaib
  • 103
  • 1
  • 11
0
votes
1 answer

how to use `heapq.merge`?

Here's the snippet I've been trying: >>> L1 = [i for i in range(10) if i % 2 == 0] >>> L2 = [j for j in range(10) if j % 2] >>> import heapq >>> [k for k in heapq.merge(L1, L2)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [k for k in heapq.merge(L1, L2,…
yasouser
  • 5,113
  • 2
  • 27
  • 41
0
votes
0 answers

Increasing the performance of python heapq?

I'm looking to improve the speed at which I can remove (pop) the smallest item from a list or array while also adding items on the fly. The maximum number of items is fixed, so I could use initialized numpy arrays but so far I've seen the best…
DivideByZero
  • 131
  • 2
  • 11
0
votes
1 answer

How to improve this code for finding the k largest elements of an array?

The following code to find the k largest elements of an array is causing a TLE error. How can I optimize it to make it run faster? import heapq for _ in range(int(input())): n,k=map(int,input().split()) …
0
votes
0 answers

to find the mean max of the heap

I number : insert number to the q
D 1 : Delete the max value from heapq
D -1: Delete the min value from heapq This is the condition so when [I 16,D 1] return [0,0] [I 7,I 5,I -5,D -1] return [7,5] I tried it with this way but some…
user11173832
0
votes
1 answer

Why does Python's heapq's tuple comparison not work as it supposed to?

So in Python when I type from heapq import heappush a=[] heappush(a,('art zero', 'let3 art zero')) heappush(a,('own kit dig', 'let2 own kit dig')) heappush(a,('art can', 'let1 art can')) print(a) it gives [('art can', 'let1 art can'), ('own kit…
linbooooo
  • 3
  • 1
0
votes
1 answer

How to store and sort dictionary in heapq?

I am getting below while storing dictionary in heapq. Does anyone know how to solve this ? '<' not supported between instances of 'dict' and 'dict' import heapq PQ = [] heapq.heappush(PQ, {"1": "animal"}) heapq.heappush(PQ, {"2":…
Atila
  • 81
  • 4
  • 9
0
votes
1 answer

Error inserting torch tensors into a heapq using duplicated priorities

How to avoid RuntimeError: bool value of Tensor with more than one value is ambiguous in this code? import torch import heapq h = [] heapq.heappush(h, (1, torch.Tensor([[1,2]]))) heapq.heappush(h, (1, torch.Tensor([[3,4]]))) It happens because the…
jperezmartin
  • 407
  • 4
  • 19
0
votes
1 answer

Is there a function in Python3 to copy the 3 smallest values in a row into a data frame, for m rows?

I'm trying to find the three smallest values for each row of a dataframe, and put them in a separate dataframe. I don't need to know which column they came from, but I do need to cycle through m rows where m might change for each dataframe I use. I…
Tom
  • 109
  • 1
  • 9
0
votes
0 answers

What does it mean for N to be small compared to the overall size of the collection?

I'm new to python-3x and I'm having a hard time understanding what it means for N to be smaller compared to the overall size of the collection: "If you are looking for the N smallest or largest items and N is small compared to the overall size of…
0
votes
1 answer

Strange interferences bewteen Heapq module and dictionary

On one hand, I have a grid defaultdict that stores the neighboring nodes of each node on a grid and its weight (all 1 in the example below). node (w nbr_node) grid = { 0: [(1, -5), (1, -4), (1, -3), (1, -1), (1, 1), (1, 3), (1, 4), (1,…
solub
  • 1,291
  • 17
  • 40
0
votes
2 answers

Python library, what does "q" stand for in heapq?

Python has this library heapq, which we can use to perform heap operations. What does "q" stand for?