priority-queue

Does PriorityQueue call sorted every time get is called?

Does PriorityQueue call sorted every time get is called? Question: The docs for queue.PriorityQueue say: The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). Does that mean that every time I use the get method of a PriorityQueue that the elements are re-sorted? That would seem inefficient. …

Total answers: 1

Python: updating priorities in a list of dictionaries

Python: updating priorities in a list of dictionaries Question: I have the following list of dictionaries: tasks = [{‘priority’: 2, ‘task’: ‘math’, ‘complete_time’: 15}, {‘priority’: 10, ‘task’: ‘french’, ‘complete_time’: 90}, {‘priority’: 5, ‘task’: ‘geography’, ‘complete_time’: 45}, {‘priority’: 2, ‘task’: ‘math’, ‘complete_time’: 100}] I would like to update this list by changing the priorities from zero …

Total answers: 2

Is it possible to pass a comparator to a PriorityQueue in python

Is it possible to pass a comparator to a PriorityQueue in python Question: I want to use a PriorityQueue to which I need to add objects of a class (say Node) which I cannot modify. I need these objects prioritized based on a field of the object. I tried adding them as tuples(node.val, node) to …

Total answers: 2

What's the difference between heapq and PriorityQueue in python?

What's the difference between heapq and PriorityQueue in python? Question: In python there’s a built-in heapq algorithm that gives you push, pop, nlargest, nsmallest… etc that you can apply to lists. However, there’s also the queue.PriorityQueue class that seems to support more or less the same functionality. What’s the difference, and when would you use …

Total answers: 3

built-in max heap API in Python

built-in max heap API in Python Question: Default heapq is min queue implementation and wondering if there is an option for max queue? Thanks. I tried the solution using _heapify_max for max heap, but how to handle dynamically push/pop element? It seems _heapify_max could only be used during initialization time. import heapq def heapsort(iterable): h …

Total answers: 2

Get list of elements currently in heap using heapq in Python?

Get list of elements currently in heap using heapq in Python? Question: This structure i have built for easier understanding and usability class PriorityQueue: """ Implements a priority queue data structure. """ def __init__(self): self.heap = [] self.count = 0 def push(self, item, priority): entry = (priority, self.count, item) heapq.heappush(self.heap, entry) self.count += 1 def …

Total answers: 1

How to implement Priority Queues in Python?

How to implement Priority Queues in Python? Question: Sorry for such a silly question but Python docs are confusing… Link 1: Queue Implementation http://docs.python.org/library/queue.html It says that Queue has a class for the priority queue. But I could not find how to implement it. class Queue.PriorityQueue(maxsize=0) Link 2: Heap Implementation http://docs.python.org/library/heapq.html Here they say that …

Total answers: 3

Can I get an item from a PriorityQueue without removing it yet?

Can I get an item from a PriorityQueue without removing it yet? Question: I want to get the next item in queue but I don’t want to dequeue it. Is it possible in Python’s queue.PriorityQueue? From the docs, I don’t see how can it be done Asked By: Jiew Meng || Source Answers: Indexing the …

Total answers: 7