因此,我希望按值对字典进行排序,并在项目值相同的情况下按字母顺序维护。但是,python字典不按字母顺序保存数据,如何解决?提前谢谢!
这是我的代码:
from collections import Counter
test = "betty bought a bit of butter but the butter was bitter"
Counter(test.split()).most_common(3)
输出:
[('butter', 2), ('a', 1), ('bitter', 1)]
所需的输出应为:
[('butter', 2), ('a', 1), ('betty', 1)]
因为那'苦涩'应该落后于贝蒂'按字母顺序。
答案 0 :(得分:3)
将heapq.nsmallest()
function与自定义密钥一起使用,传入否定计数以反转该部分的排序,并按字母顺序返回密钥本身:
import heapq
top3 = heapq.nsmallest(
3, Counter(test.split()).items(),
key=lambda kv: (-kv[1], kv[0]))
Counter.most_common()
方法使用heapq.nlargest()
为它提供一个小于字典中键数的参数,并且只使用计数来确定顺序,所以在关系的情况下,顺序是随心所欲。以上内容与您的特定排序顺序相同。就像Counter.most_common()
一样,这是一个O(NlogK)解决方案(N是计算的项目数,K是您想要输出的项目数)。
演示:
>>> import heapq
>>> from collections import Counter
>>> test = "betty bought a bit of butter but the butter was bitter"
>>> heapq.nsmallest(3, Counter(test.split()).items(), key=lambda kv: (-kv[1], kv[0]))
[('butter', 2), ('a', 1), ('betty', 1)]
答案 1 :(得分:1)
或者 - 您可以混合Counter
和OrderedDict
:
from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
pass
test = "betty bought a bit of butter but the butter was bitter"
mc = OrderedCounter(sorted(test.split())).most_common(3)
# [('butter', 2), ('a', 1), ('betty', 1)]