#### 问题:
怎样实现一个按优先级排序的队列?并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素
#### 解决问题:
下面的类利用 `heapq` 模块实现了一个简单的优先级队列
```python
import heapq
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0
def push(self, item, priority):
# -priority 越小,优先级越高
heapq.heappush(self._queue, (-priority, self._index, item))
self._index += 1
def pop(self):
return heapq.heappop(self._queue)[-1]
```
下面是它的使用方式:
```
>>> 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('grok'), 1)
>>> q.pop()
Item('bar')
>>> q.pop()
Item('spam')
>>> q.pop()
Item('foo')
>>> q.pop()
Item('grok')
>>>
```
##### 仔细观察可以发现,第一个`pop()`操作返回优先级最高的元素。另外注意到如果两个有着相同优先级的元素(`foo`和`grok`),`pop`操作按照它们被插入到队列的顺序返回的。
*****
*****
原文地址:
> https://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p05_implement_a_priority_queue.html