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, How to efficiently modify the minimum element in heapq?

I am using heapq in python 3.7 I have two questions about heapq: I don't know how to keep the heap invariant efficiently if I just want to modify the min element. And here is my implementation. (It is quite slow) q=…
Silvan
  • 65
  • 7
0
votes
2 answers

How to merge k sorted arrays, solution didn't work allowing duplicates.!

Given k sorted arrays of size n each, merge them and print the sorted output. The algorithm I followed is iterate of over each array pick the ith index in k arrays insert() in minheap delMin() and append result array. from heapq import…
Anu
  • 3,198
  • 5
  • 28
  • 49
0
votes
1 answer

heapq custom compareTo

I'm trying to define a custome method to make an ordered insert into a priority queue in python but not getting the expected results. Once defined the insert method into the queue like the following: def insert(self, node): if isinstance(node,…
jupcan
  • 436
  • 3
  • 7
  • 17
0
votes
0 answers

Why are the elements in my heapq not ordered python?

I am using a simple heapq in python with custom elements on which I implemented the lt function. class Edge: def __init__(self, cost, u, v): self.u = u self.v = v self.cost = cost def weight(self): w =…
Traoré Moussa
  • 433
  • 1
  • 6
  • 22
0
votes
0 answers

heapq: What is an elegant way to use customized priority key with associated data that are not comparable?

As shown in a basic example in the doc, one can always use a tuple so that the first element is compared as the priority key. However, what if the associated data are not comparable? What would be an elegant way to resolve this? For example, import…
xuhdev
  • 8,018
  • 2
  • 41
  • 69
0
votes
1 answer

Heapq with equal priority

I am trying to create a hip of event. Thus, I have define a class Event which is inherited by my different events. class Event: def __init__(self, last_instant): self.last_instant = last_instant # That's the prio criteria class…
Mathieu
  • 5,410
  • 6
  • 28
  • 55
-1
votes
1 answer

Python heapq pushed task is missing a parameter

With the below declared class: runner_pq = [] p_count = 0 class RunThread(threading.Thread): def __init__(self, func, **kwargs): self._func = func self._kwargs = kwargs threading.Thread.__init__(self, name="IVU") …
Ben Jafari
  • 19
  • 4
-1
votes
2 answers

Why heappush is taking 3 pararmeter?

Why heappush is taking 3 parameters arr[i].data, i and arr[i]. Why is it taking i as parameter It usually takes one one parameter It is the code for merging k sorted link list def mergeKLists(arr,K): # code here # return head of merged list heap…
-1
votes
1 answer

Python heapq sorting for string list not correct?

I am wondering why not the heapq in python sort the strings in lexical order when using heappush? Below is the code I tried. Anyone know why they print different order? import heapq string_list = ["TIA","AXA","JFK"] result = [] for each in…
windshine
  • 11
  • 1
-1
votes
1 answer

heappush behavior in python

I have an array arr = [1,2,1,3]. When I use heappush function to push the array elements as they are into a heap, the order of stays exactly the same as it is in the array: arr = [1,2,1,3] heap = [] for i in range(len(arr)): heapq.heappush(heap,…
Amir Jalilifard
  • 2,027
  • 5
  • 26
  • 38
-1
votes
1 answer

Understanding heapq sorting algorithm

I am reading the book, "Python from Novice to Expert" by Magnus Lie Hetland (Third Edition) and came across Heaps. There he discusses the sorting order of a heap list as "the order of the elements is important (even though it may look a bit…
eladgl
  • 69
  • 8
-2
votes
1 answer

Why cant I create heap out of python list

import heapq x=[1,4,2] heapq.heapify(x) print(x) The result I get is [1,4,2], what am I missing?
-2
votes
2 answers

how do i use this code to find the top 5 max numbers in a file if each number has a string next to it?

from heapq import nlargest with open("winnernum.txt", "r") as f: numbers = [float(line.rstrip()) for line in f.readlines()] largest = nlargest(5, numbers) print(largest) I have tested this code and it works if there is no string next to…
katy
  • 19
  • 3
-2
votes
1 answer

what heap pop returns?

I am confused in heap pop output. import heapq heapq.heappush(PQ, ['b', 0.95]) heapq.heappush(PQ, ['d', 0.72]) heapq.heappush(PQ, ['e', 1.75]) a = heapq.heappop(PQ) print (a) It returns : ['b', 0.95] Why it's not returning ['d', 0.72]
Atila
  • 81
  • 4
  • 9
-2
votes
2 answers

heapq.merge() iterator goes through more items than in the lists

Following the docs of heapq.merge() - I get very strange results, and cannot find what am I doing wrong... The setup is as follows: I am using heapq.merge() to sort multiple lists. Tested with 2 ~ 8 list iterators, and the results are exactly the…
rubmz
  • 1,947
  • 5
  • 27
  • 49
1 2 3 4 5
6