The easiest way was to use ** PriorityQueue ** or ** bisect **, which supports binary search. I'd like to find out how to set the comparison function by myself (whether it can be done) and how the performance is.
bisect
from bisect import insort
q = []
insort(q, 1)
insort(q, 10)
insort(q, 2)
insort(q, 5)
insort(q, 3)
print(q) # [1, 2, 3, 5, 10]
PriorityQueue
import Queue as Q
q = Q.PriorityQueue()
q.put(1)
q.put(10)
q.put(2)
q.put(5)
q.put(3)
# 1, 2, 3, 5,Output in order of 10
while q.qsize() > 0:
print(q.get())
Recommended Posts