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
2
votes
2 answers

How to iterate heapq without losing data?

In Python 3 I'm using heapq like so: import heapq heap = [3] heapq.heapify(heap) heapq.heappush(heap, 5) # Push more values... # I can iterate heap like so, but by the end it will be empty: while (heap): curr = heapq.heappop(heap) # Do…
kebab-case
  • 1,732
  • 1
  • 13
  • 24
2
votes
2 answers

How to find position of an element in Heapq

I am trying to implement HeapQ using Python but im stuck with this scenario , where i need to get the position of the key in the queue ? . I hit the wall trying to solve this. Any tips will be appreciated.
Harun Guna
  • 129
  • 1
  • 1
  • 7
1
vote
1 answer

Efficiently convert list of probabilities in a list of 0/1 by taking a % of highest probabilities without reindexing

Problem Given a huge array of probabilities and the percentages to take probabilities = [0.1, 0.4, 0.7, 0.2, 0.9, 0.5, 0.6] N_percentages = [20, 30, 40] # Percentage of the list size I want to efficiently compute {20:[0, 0, 0, 0, 1, 0, 0], 30:[0,…
1
vote
0 answers

What's the time complexity of the following code, heapq library

I wanted to do the complexity analysis of this algorithm, however I don't know the complexity of heap. I would think it could be O(V + E log E) but I don't know. def dijkstra(start, goal, graph): queue = [] heappush(queue, (0, start)) …
1
vote
2 answers

Sorting Elements in a List Based on Values

I have a dictionary and I need the keys to be sorted according to their corresponding values. But the keys should not change the preset order when sorting is applied The below example shows exactly what I am trying to do. The code returns the keys…
1
vote
0 answers

how to declare specification of list of list for heapify in jitclass mode

I am trying to use heapq inside a function of jitclass , the following is my code: spec = [('sese', types.ListType(types.ListType(types.int32)))] @jitclass(spec) class CK (): def __init__(self): self.sese = [[0,0,0,0]] def main_…
Axd
  • 45
  • 2
1
vote
1 answer

Python heapq providing unexpected output

I have a simple python script that aims to read all rows in a csv file and perform a heap sort based on the second element of each row. Here is my function to read the file: def read Processes(): file = open('Project1/Processes/sample.csv',…
Ben Gagnon
  • 11
  • 2
1
vote
1 answer

maxHeap python converts to min heap after poping element

Trying to understand the max heap in python. Once I pop the element the elements are arranged as min heap. import heapq a=[3,2,1,4,9] heapq._heapify_max(a) # This createa a binary tree with max val at the root print(a) # This should be…
Harsha Vardhan
  • 119
  • 1
  • 12
1
vote
1 answer

Sort and select from a complex nested dictionary using heapq

I have the following nested dictionary: a={'2020-12-08': {'navi.o_efx': {'coint_value': 0.923033, 'hl_value': 0.475025}, 'stm.n_efx': {'coint_value': 0.915424, 'hl_value': 0.294162}, 'kioo.o_efx': {'coint_value': 0.92575,…
Prateek Daniels
  • 451
  • 1
  • 4
  • 19
1
vote
1 answer

What is the difference between heappop and pop(0) on a sorted list?

I have a list stored in this format: [(int, (int, int)), ...] My code looks like this for the first case. heap.heappush(list_, (a, b)) # b is a tuple while len(list_): temp = heap.heappop(list_)[1] Now my ideal implementation would…
Karl Stark
  • 11
  • 4
1
vote
2 answers

Heapop and Heappush with a regular list (non-heapified list)

So I've seen various posts where users get "unusual/unexpected" results when trying to heappop from a regular list. (ex: Unusual result from heappop?) The solution of course is to heapify it first. However, for this LeetCode solution heapq methods…
chadlei
  • 179
  • 1
  • 11
1
vote
2 answers

How does heapq resolve equal values?

Coming from Java, I am trying to implement A* algorithm in python and I'm having trouble sorting vertices in my graph that have equal f scores. I'm trying to do so using heapq, and after some debugging I noticed if I would like to push a vertex that…
Timbwa
  • 55
  • 1
  • 10
1
vote
0 answers

Why does heapq.heapify not alter a list when a slice thereof is passed to it?

According to a previous answer Slicing lists does not generate copies of the objects in the list; it just copies the references to them. However, when I run the following: from heapq import heapify import random as r def heapfiddling(a): …
1
vote
1 answer

How to add list to heapq in python

How can i add the input list to heap directly?,Where some of the inbuild function used to push,get min,extract min but how to extract the maximum from the heap. some functions like.. heapify(iterable) :- This function is used to convert the…
bennyl
  • 15
  • 1
  • 7
1
vote
1 answer

How does heapq.merge() work with infinite generators?

I want to understand how heapq.merge() works with infinite generators. Consider this example: >>> from heapq import merge >>> from itertools import count >>> m = merge(count(0, 2), count(1, 2)) >>> for _ in range(10): ... …
planetp
  • 14,248
  • 20
  • 86
  • 160