Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JessicaWachtel/ee7f77b7b7d6c86e9b0bf660ae14c907 to your computer and use it in GitHub Desktop.

Select an option

Save JessicaWachtel/ee7f77b7b7d6c86e9b0bf660ae14c907 to your computer and use it in GitHub Desktop.
import json
from collections import defaultdict
def f1(path):
with open(path, 'r') as f:
return json.load(f)
def f2(items):
result = defaultdict(list)
for i in items:
k = i.get('category', 'Unknown')
result[k].append(i)
return result
def f3(d):
out = {}
for k, v in d.items():
s = sum(x['value'] for x in v)
m = s / len(v)
out[k] = m
return out
def runner():
data = f1('data.json')
grouped = f2(data)
stats = f3(grouped)
for k, v in stats.items():
print(f"{k}: {v:.2f}")
if __name__ == '__main__':
runner()
@tamimbook
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment