如何通过计数器对Counter.mostCommon
的结果进行排序,然后对值进行排序?
我的原始代码:
from collections import Counter
for counter in Counter("abcdefg").most_common(3):
print(counter[0], counter[1])
每次输出都不同,因为每个值的计数为1。 有时它是
a 1
b 1
e 1
有时
b 1
d 1
f 1
等
我想要这个:
a 1
b 1
c 1
我也尝试对生成的元组进行排序::
from collections import Counter
for counter in sorted(Counter("abcdefg").most_common(3), key=lambda x: x[0]): print(counter[0], counter[1])
并对字符串进行排序
from collections import Counter
for counter in Counter(sorted("abcdefg")).most_common(3): print(counter[0], counter[1])
但我得到了同样不可预测的结果
答案 0 :(得分:4)
这里的问题是Counter
dicts是无序的,most_common
并不关心密钥。要做到这一点,你需要对dict的项目进行排序,然后拉出最常见的3项。
counter = Counter('abcdef')
most_common = sorted(counter.items(), key=lambda pair: (-pair[1], pair[0]))
这将首先对-pair[1]
(计数)进行排序。由于负数,首先出现更高的计数。接下来,我们按pair[0]
(密钥)排序,按正常递增顺序按字典顺序排序。
从这里开始,你需要切掉你想要的物品......
most_common[:3]
或者,我们可以从the source code中删除一页并重新实施most_common
以考虑密钥。
import heapq as _heapq
def most_common(counter, n=None):
'''List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
'''
# Emulate Bag.sortedByCount from Smalltalk
sort_key = lambda pair: (-pair[1], pair[0])
if n is None:
return sorted(counter.iteritems(), key=sort_key)
return _heapq.nsmallest(n, counter.iteritems(), key=sort_key)