python:优先级队列

论坛 期权论坛 脚本     
匿名技术用户   2020-12-29 14:54   11   0
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()

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP