在Python中基于频率对计数器进行排序,重新组织

时间:2017-09-16 21:49:18

标签: python data-processing

我的代码如下所示:

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'

1 个答案:

答案 0 :(得分:2)

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

计数是Counter个实例,它理解most_common方法。

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

计数现在是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)]