My code looks like this:
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)
Here is the 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"}
The corresponding output:
{u'1': 2, u'0': 5, u'3': 4, u'4': 2}
What I'd like to do is sort this output, so that it's rendered as:
{ u'0': 5, u'3': 4, u'4': 2, u'1': 2}
Thus far I tried counts = counts.most_common()
, but it didn't work. I got the following error:
AttributeError: 'dict' object has no attribute 'most_common'
counts is a
Counter
instance, which understandsmost_common
method.counts is now a
dict
, which doesn't understandmost_common
.You just need to apply
most_common
first, and then apply the treshold: