Skip to content

Instantly share code, notes, and snippets.

@dkmonaghan
Last active December 7, 2020 20:06
Show Gist options
  • Save dkmonaghan/05af10af19044e194fdeca3419653f4d to your computer and use it in GitHub Desktop.
Save dkmonaghan/05af10af19044e194fdeca3419653f4d to your computer and use it in GitHub Desktop.
Analyses Tesco-Customer-Data.json and gives you useless statistics about your order history
#!/usr/bin/python3
import json
import math
with open('Tesco-Customer-Data.json') as json_file:
data = json.load(json_file)
orders = []
for purchase_block in data['Purchase']:
for purchase in purchase_block:
orders.append(purchase)
items = {}
for order in orders:
for product in order['product']:
if product['name'] in items:
items[product['name']]['money_spent'] += float(product['price'])
items[product['name']]['quantity'] += float(product['quantity'])
else:
items[product['name']] = {}
items[product['name']]['name'] = product['name']
items[product['name']]['money_spent'] = float(product['price'])
items[product['name']]['quantity'] = float(product['quantity'])
to_delete = []
for key, item in items.items():
if item['quantity'] == 0:
to_delete.append(key)
for key in to_delete:
del items[key]
most_expensive = sorted(items.values(), key=lambda k: k['money_spent'], reverse=True)
most_bought = sorted(items.values(), key=lambda k: k['quantity'], reverse=True)
print("You spent the most money on...\n")
for item in most_expensive[:10]:
print("%s (%s items for £%s)" % (item['name'], math.ceil(item['quantity']), round(item['money_spent'], 2)))
print("\n\nAnd you bought these items most frequently...\n")
for item in most_bought[:10]:
print("%s (%s items for £%s)" % (item['name'], math.ceil(item['quantity']), round(item['money_spent'], 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment