擅长:python、mysql、java
<p><code>sortedcontainers</code>包中的<code>SortedDict</code>用作带有排序键的<code>dict</code>对象。(<a href="http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html" rel="nofollow noreferrer">http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html</a>)</p>
<p>要按<strong>值排序</strong>,在<code>sortedcollections</code>中还有<code>ValueSortedDict</code>。(<a href="http://www.grantjenks.com/docs/sortedcollections/valuesorteddict.html" rel="nofollow noreferrer">http://www.grantjenks.com/docs/sortedcollections/valuesorteddict.html</a>)</p>
<p>否则,像这样的纯<code>builtin</code>函数可能会有帮助:</p>
<pre><code>def sort_dict_by_values(dictionary):
return dict(sorted(list(dictionary), key=lambda tup: tup[1], reverse=True))
</code></pre>