引言:在python中,对于优先队列的使用,其实是非常多的。虽然你可以不去考究这个类的内部的方法是怎么运行的,而是直接去调用对象的方法,对于做到这一点你只需要去阅读这个方法的使用说明便可以了。但是如果我们了解优先队列内部的运行规则,我想我们可以把对于知识的理解下沉到类似数学证明中“公理”的位置。
import itertools
from heapq import *
class PriorityQueue(object):
def __init__(self):
self.pq = [] # list of entries arranged in a heap 点集 ?list of task arranged in a heap
self.entry_finder = {} # mapping of tasks to entries 点和自身属性组成的字典
self.REMOVED = '<removed-task>' # placeholder for a removed task
self.counter = itertools.count() # unique sequence count
#print(self.counter)
def add_task(self, task, priority=0):
'Add a new task or update the priority of an existing task' #更新的就是最新输入的
if task in self.entry_finder:
self.remove_task(task)
count = next(self.counter)
entry = [priority, count, task]
self.entry_finder[task] = entry
heappush(self.pq, entry) #列表,把新的任务送进“堆”里面,当然排序方式按照“堆”的排序规则来,
#如果任务为'<removed-task>'则会在堆(heap)的内部被直接删除掉
def remove_task(self, task):
'Mark an existing task as REMOVED. Raise KeyError if not found.'
entry = self.entry_finder.pop(task) #在这里用字典的方法pop进行删除
entry[-1] = self.REMOVED #如果任务为'<removed-task>'则会在堆(heap)的内部被直接删除掉
def pop_item(self):
'Remove and return the lowest priority task. Raise KeyError if empty.'
while self.pq:
priority, count, task = heappop(self.pq)
if task is not self.REMOVED: #删除的是优先级最低的,但是在heap堆中需要**被删除**'<removed-task>'的点应该在队中也被删除了
del self.entry_finder[task]
# print(task, priority)
return task, priority
raise KeyError('pop from an empty priority queue')
def __str__(self):
return str([entry for entry in self.pq if entry[2] != self.REMOVED])
pq = PriorityQueue()
pq.add_task(pq.REMOVED, -100)
pq.add_task(1, -75) #(条目编号,优先级)
pq.add_task(2, -50)
pq.add_task(3, -50)
pq.add_task(4, -90)
pq.add_task(pq.REMOVED, -25)
print(pq.pop_item())
print(pq)
if __name__ == '_main__':
console = []
在上面代码的关键位置,我已经给出了注释,我想如果你了解python的基础,应该会看明白。
其中值得注意的一点就是:当”task"被标注为"remove-task",task会被从队列中删除,也就是self.pq中删除掉。