如果我想打印PriorityQueue
中的值,我该怎么办?我扩展了PriortiyQueue
,并且可以访问基础列表queue
。但是,当我执行for循环时,我会得到添加项目的顺序
# the ADT PriorityQueue
class ReadyQueue(PriorityQueue):
...
def listAll(self):
print("PID \t Name \t Status \t Priority")
print("=" * 42)
for _, _, pcb in self.queue:
print(pcb)
# ADT
class PCB:
...
def __lt__(self, other):
selfPriority = (self.priority, self.creationTime)
otherPriority = (other.priority, other.creationTime)
return selfPriority < otherPriority
# in the main function
q = ReadyQueue()
q.enqueue(PCB("p1", 0, None, q))
q.enqueue(PCB("p8", 6, None, q))
q.enqueue(PCB("p2", 1, None, q))
q.enqueue(PCB("p0", 1, None, q))
q.enqueue(PCB("p6", 1, None, q))
q.enqueue(PCB("p4", 1, None, q))
q.enqueue(PCB("p3", 2, None, q))
q.listAll()
输出:
PID Name Status Priority
==========================================
0 p1 ready_s 0
3 p0 ready_s 1
2 p2 ready_s 1
1 p8 ready_s 6
4 p6 ready_s 1
5 p4 ready_s 1
6 p3 ready_s 2
注意优先权不对。虽然他们也没有出现在创作顺序中......我是否想念一些东西?
答案 0 :(得分:2)
PriorityQueue使用heapq来推送和弹出元素http://docs.python.org/library/heapq.html
所以,队列是堆,而不是排序列表。