基于计数器排序,基于频率重新组织,Python

Sort a counter based, re-organize based on frequency, in Python

我的代码如下所示:

with open('toy_two.json', 'rb') as inpt:

    dict_hash_gas = list()
    for line in inpt:
        resource = json.loads(line)
        dict_hash_gas.append({resource['first']:resource['second']})

# Count up the values
counts = collections.Counter(v for d in dict_hash_gas for v in d.values())

# Apply a threshold
counts = {k:v for k,v in counts.iteritems() if v > 1}

print(counts)

这是数据:

{"first":"A","second":"1","third":"2"} 
{"first":"B","second":"1","third":"2"} 
{"first":"C","second":"2","third":"2"} 
{"first":"D","second":"3","third":"2"} 
{"first":"E","second":"3","third":"2"} 
{"first":"F","second":"3","third":"2"} 
{"first":"G","second":"3","third":"2"} 
{"first":"H","second":"4","third":"2"} 
{"first":"I","second":"4","third":"2"} 
{"first":"J","second":"0","third":"2"} 
{"first":"K","second":"0","third":"2"} 
{"first":"L","second":"0","third":"2"} 
{"first":"M","second":"0","third":"2"} 
{"first":"N","second":"0","third":"2"} 

对应输出:

{u'1': 2, u'0': 5, u'3': 4, u'4': 2}

我想做的是对这个输出进行排序,使其呈现为:

{ u'0': 5, u'3': 4, u'4': 2, u'1': 2}

到目前为止,我尝试了 counts = counts.most_common(),但没有成功。我收到以下错误:

AttributeError: 'dict' object has no attribute 'most_common'
# Count up the values
counts = collections.Counter(v for d in dict_hash_gas for v in d.values())

counts 是一种 Counter instance, which understands most_common 方法。

# Apply a threshold
counts = {k:v for k,v in counts.iteritems() if v > 1}

counts 现在是 dict,不理解 most_common

只需要先申请most_common,再申请门槛:

data = [{"first":"A","second":"1","third":"2"} ,
    {"first":"B","second":"1","third":"2"} ,
    {"first":"C","second":"2","third":"2"} ,
    {"first":"D","second":"3","third":"2"} ,
    {"first":"E","second":"3","third":"2"} ,
    {"first":"F","second":"3","third":"2"} ,
    {"first":"G","second":"3","third":"2"} ,
    {"first":"H","second":"4","third":"2"} ,
    {"first":"I","second":"4","third":"2"} ,
    {"first":"J","second":"0","third":"2"} ,
    {"first":"K","second":"0","third":"2"} ,
    {"first":"L","second":"0","third":"2"} ,
    {"first":"M","second":"0","third":"2"} ,
    {"first":"N","second":"0","third":"2"}]

from collections import Counter
c = Counter(int(d["second"]) for d in data)
print(c)
# Counter({0: 5, 3: 4, 1: 2, 4: 2, 2: 1})
print(c.most_common())
# [(0, 5), (3, 4), (1, 2), (4, 2), (2, 1)]
print([(value, count) for value, count in c.most_common() if count > 1])
# [(0, 5), (3, 4), (1, 2), (4, 2)]