很抱歉这样一个愚蠢的问题,但Python文档令人困惑.
链接1:队列实施 http://docs.python.org/library/queue.html
它说那个队列有一个优先队列的构造.但我找不到如何实现它.
class Queue.PriorityQueue(maxsize=0)
Run Code Online (Sandbox Code Playgroud)
链接2:堆实现 http://docs.python.org/library/heapq.html
在这里,他们说我们可以使用heapq间接实现优先级队列
pq = [] # list of entries arranged in a heap
entry_finder = {} # mapping of tasks to entries
REMOVED = '<removed-task>' # placeholder for a removed task
counter = itertools.count() # unique sequence count
def add_task(task, priority=0):
'Add a new task or update the priority of an existing task'
if task in entry_finder:
remove_task(task)
count = next(counter)
entry = [priority, count, task]
entry_finder[task] = entry
heappush(pq, entry)
def remove_task(task): …Run Code Online (Sandbox Code Playgroud)