python:优先级队列

import heapq
class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0
    def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index += 1
    def pop(self):
        return heapq.heappop(self._queue)[-1]
q = PriorityQueue()
q.push('foo', 1)
q.push('yun', 5)
q.push('bar', 5)
q.pop()
q.pop()

猜你喜欢

转载自blog.csdn.net/qq_26948675/article/details/82805454