Reputation: 37
I need to find the top 3 keys that have the most amount of occurrences in a JSON file. I found the file to have 41 unique keys (out of thousands of objects) and now I have to find the number of times each of those keys occurs in a JSON file and return the top 3 keys with the amount of times each key is repeated in descending order.
Here is the code I've done already. It's here that I've managed to get the amount of unique keys from all of the objects.
import json
with open('users.json') as f:
data = json.load(f) #loads JSON data into dictionary
unique_users = set(item["user_id"] for item in data) # gives unique
users in data
users = list(unique_users) # 41 users list
Expected result format is something like:
1. user234: 456
2. user245: 345
3. user 3453: 154
Upvotes: 0
Views: 2152
Reputation: 20490
You can use collections.Counter to create the frequency dictionary, and then pick the top 3 elements from that frequency table using Counter.most_common
from collections import Counter
import json
#Get dictionary
with open('users.json') as f:
data = json.load(f) #loads JSON data into dictionary
#Get all user_id from the dictionary and create a frequency counter
counter = Counter(item["user_id"] for item in data)
#Get top 3 using most_common
top_3 = counter.most_common(3)
for key, value in top_3:
print('{}: {}'.format(key,value))
For example
from collections import Counter
from itertools import product
import json
#Assuming these are the list of user id's we got from json
counter = Counter(['user_1','user_1','user_1','user_1','user_2','user_2','user_2','user_3','user_3','user_4' ])
#Get top 3 using most_common
top_3 = counter.most_common(3)
for key, value in top_3:
print('{}: {}'.format(key,value))
The output will be
user_1: 4
user_2: 3
user_3: 2
Upvotes: 0
Reputation: 27505
Using collections.Counter
import json
from collections import Counter
with open('users.json') as f:
data = json.load(f)
user_counter = Counter(item["user_id"] for item in data)
for i, (user, count) in enumerate(user_counter.items()):
print(f"{i}. {user}: {count}")
Upvotes: 1