you can use this method as a trick multiply by -1 to inverse the order :
q = PriorityQueue()
q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
next_item = q.get()
print(next_item)
output:
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
Answer from Ghassen on Stack OverflowPython priority queue pops in descending order - Stack Overflow
How do I remove the top element from priorityQueue
python - Can I get an item from a PriorityQueue without removing it yet? - Stack Overflow
A generic priority queue for Python - Stack Overflow
Videos
you can use this method as a trick multiply by -1 to inverse the order :
q = PriorityQueue()
q.put((2*(-1), 'code'))
q.put((1*(-1), 'eat'))
q.put((3*(-1), 'sleep'))
while not q.empty():
next_item = q.get()
print(next_item)
output:
(-3, 'sleep')
(-2, 'code')
(-1, 'eat')
There is no way to change the behavior of the queue class. I would decorate the items before queuing them, and un-decorate them after retrieving them, something like this:
def decorated(item):
return (-item[0],) + item
def undecorated(item):
return item[1:]
That gives you items that will queue up in the right order:
>>> decorated(1, 'eat')
(-1, 1, 'eat')
>>> undecorated(-1, 1, 'eat')
(1, 'eat')
That way you can say q.put(decorated(item)) instead of q.put(item). Analogously, you retrieve the item by saying undecorated(q.get()) instead of q.get(). After this, the rest of your code keeps working as it did before.
I am implementing a priority queue using priorityQueue from queues. However, I canโt seem to get the right method to delete the top element from this queue. So far I have tried delete, pop and remove. I get errors for them all saying priorityQueue has no attribute like remove, delete or pop.
Please help! This should be fairly simple but itโs driving me crazy
If a is a PriorityQueue object, You can use a.queue[0] to get the next item:
from queue import PriorityQueue
a = PriorityQueue()
a.put((10, "a"))
a.put((4, "b"))
a.put((3,"c"))
print(a.queue[0])
print(a.queue)
print(a.get())
print(a.queue)
print(a.get())
print(a.queue)
output is :
(3, 'c')
[(3, 'c'), (10, 'a'), (4, 'b')]
(3, 'c')
[(4, 'b'), (10, 'a')]
(4, 'b')
[(10, 'a')]
but be careful about multi thread access.
If you want next element in the PriorityQueue, in the order of the insertion of the elements, use:
for i in range(len(queue.queue)):
print queue.queue[i]
this will not pop anything out.
If you want it in the priority order, use:
for i in range(len(queue.queue)):
temp = queue.get()
queue.put(temp)
print temp
If you are using a tuple, instead of a single variable, replace temp by:
((temp1,temp2))
You can use Queue.PriorityQueue.
Recall that Python isn't strongly typed, so you can save anything you like: just make a tuple of (priority, thing) and you're set.
When using a priority queue, decrease-key is a must-have operation for many algorithms (Dijkstra's Algorithm, A*, OPTICS), I wonder why Python's built-in priority queue does not support it. None of the other answers supply a solution that supports this functionality.
A priority queue which also supports decrease-key operation is this implementation by Daniel Stutzbach worked perfectly for me with Python 3.5.
from heapdict import heapdict
hd = heapdict()
hd["two"] = 2
hd["one"] = 1
obj = hd.popitem()
print("object:",obj[0])
print("priority:",obj[1])
# object: one
# priority: 1