I'm extremely confused about the different behavior of PriorityQueue when the item is an integer versus a string. But before addressing that, I'd like to understand the following behavior (using items as integers).
Suppose I have a priority queue with the following data (for each element, the first value is the priority and the second value is an item):
Image 1:
After I execute the following commands:
pq.put(pq.get())
The elements become sorted as follows:
Image 2:
Why have some elements changed places? What's going on with the sorting?
Here's the code to reproduce these screenshots from the debugger:
from sys import maxsize
from queue import PriorityQueue
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 15, 16]
INFINITY = maxsize
minDist = {k: INFINITY for k in items}
minDist[3] = 0
pq = PriorityQueue(len(minDist))
for v in minDist.keys():
pq.put([minDist[v], v])
# At this point, pq has the elements as shown in Image 1
pq.put(pq.get())
# Now, pq has elements scrambled as shown in Image 2
LESSONS LEARNED
Based on the investigations and discussions below, each call to pq.put(pq.get()) will rearrange the priority queue data structure. It's not clear if there's a way to control that. Please comment below if you know how!
My specific problem seems to be related to the sorting of items. As shown in Images 1 and 2, the items are integers from 1 to 17. If they're converted to strings (i.e., "1", "2", ..., "17"), I was getting a different behavior from the main function in my original code. However, if I use zfill(2) for each key, so that I have "01", "02", ..., "17", I'm able to get the same final results as what I get if I use integers instead.

