擅长:python、mysql、java
<p>根据<a href="https://docs.python.org/3.2/library/queue.html?highlight=priorityqueue" rel="nofollow">docs</a>:</p>
<blockquote>
<p>The lowest valued entries are retrieved first (the lowest valued entry
is the one returned by sorted(list(entries))[0]). A typical pattern
for entries is a tuple in the form: (priority_number, data).</p>
</blockquote>
<p><strong>更新:</strong></p>
<p>可以创建三元组以避免重复,如下所示:</p>
<pre><code>>>> import queue
>>> class Foo(object):
... def __init__(self, bar):
... self.bar = bar
...
>>> f1 = Foo('foo')
>>> f2 = Foo('bar')
>>> f3 = Foo('baz')
>>> f4 = Foo('bar')
>>> q = queue.PriorityQueue()
>>> q.put((5,0,f1))
>>> q.put((3,0,f2))
>>> q.put((3,1,f3))
>>> q.put((1,0,f4))
</code></pre>
<p>或者(可能更干净),实现<a href="http://www.rafekettler.com/magicmethods.html#comparisons" rel="nofollow">magic methods</a>:</p>
^{pr2}$