2

The documentation for the Queue.PriorityQueue class in Python is shown here:

https://docs.python.org/2/library/queue.html#Queue.PriorityQueue

It says, "A typical pattern for entries is a tuple in the form: (priority_number, data)"

However, when I try to input basic data into a Priority Queue, I get:

from Queue import PriorityQueue
pq = PriorityQueue()
pq.put(1, "one")
pq.put(2, "two")
pq.put(3, "three")
pq.get()
1
pq.get()
2
pq.get()
3

Isn't the first value in pq.put() the priority, and the second value is the data? It seems like this is reversed. Then when I try to input (data, priority), I get:

from Queue import PriorityQueue
pq = PriorityQueue()
pq.put("two", 4)
pq.put("one", 2)
pq.put("three", 6)
pq.get()
'one'
pq.get()
'three'
pq.get()
'two'

This not in correct order, because it should output, "one, two, three" Any thoughts?

Bach
  • 6,145
  • 7
  • 36
  • 61
user3562967
  • 151
  • 3
  • 12
  • possible duplicate of [How to put items into priority queues?](http://stackoverflow.com/questions/9289614/how-to-put-items-into-priority-queues) – thefourtheye Apr 23 '14 at 05:08

1 Answers1

2

The first value is the priority. The lowest priority comes out of the queue first, so you first example makes sense. The second example is correct also, because in alphabetical order "one" < "three" < "two". It's treating your strings as the priorities and sorting by them. As it says at the page you linked:

"The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0])."

Also you may want to be putting tuples into the queue, I'm not sure, e.g.

pq.put((1, "some data"))
pq.put((2, "some lower priority data"))
James King
  • 6,229
  • 3
  • 25
  • 40
  • Oh alright I think I get it, so do the "one" and "two" strings in: pq.put(1, "one"), pq.put(2, "two") get completely ignored? I thought that the priority number was what got ignored, and pq.get() only returned the data – user3562967 Apr 23 '14 at 05:49
  • Yes I think the "one" and "two" strings get ignored. Any object can be put into the queue and the get method returns the queued objects using their natural sort order. The PriorityQueue class doesn't itself know about "priorities"; you simulate those with the (priority, data) tuple. Useful example here: http://my.safaribooksonline.com/book/programming/python/9780132778633/data-structures/ch02lev1sec5 – James King Apr 23 '14 at 06:06
  • I think the second argument you were passing to `put` is being interpreted as the optional argument `block` (which is being evaluated as true). – James King Apr 23 '14 at 07:25
  • If this answer meets your needs please consider accepting it by clicking the check mark button. – James King Apr 23 '14 at 07:25
  • The tuple priority queue is working nicely. However I need to make sure that dequeuing tuples with the same priorty, e.g. (1, "some data), (1, "other data"), will do so in the same order every time. Do you know if there is a tiebreaker for tuples with same priority? I ran some tests in python and it seems like the tiebreaker is FIFO – user3562967 Apr 24 '14 at 02:52
  • Based on the documentation I don't think there is a fifo tiebreaker, it always goes in the order of the tuples. So (1,"a") will dequeue before (1,"b") regardless of insert order. If you need a fifo tiebreak you could add a timestamp as the second element of a 3-tuple. – James King Apr 24 '14 at 05:19