标签:ret 优先 index for format item span 一个 pre
1.5 heapq模块实现一个优先级队列
#实现优先级队列 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] def __get__(self):#get(),set() return self._queue class Item: def __init__(self,name): self.name=name def __repr__(self): return ‘Item({!r})‘.format(self.name) q=PriorityQueue() q.push(Item(‘foo‘),1) q.push(Item(‘bar‘),5) q.push(Item(‘spam‘),4) q.push(Item(‘grop‘),1) q.pop()#bar q.pop()#spam q.pop()#foo print(q.__get__())
标签:ret 优先 index for format item span 一个 pre
原文地址:https://www.cnblogs.com/heyaqiong/p/10877699.html