如何按字典顺序(通过计数器,然后按值)对Counter.mostCommon(n)的结果进行排序?

时间:2015-11-04 18:45:48

标签: python sorting python-3.x

如何通过计数器对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])

但我得到了同样不可预测的结果

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)
相关问题