我有这个读取 json 文件的 python 脚本,挑选出“销量”最多的汽车(这部分已经完成),但现在我需要弄清楚如何在我们的 json 文件示例中找到销量最高的年份(car_year) 2002 年售出 296 辆汽车,不像 2007 年售出 264 辆汽车,我想出了如何汇总 json 文件的所有“car_sales”,但我需要找到销售额最高的年份
Python脚本:
#!/usr/bin/env python
import json
data = json.load(open('/home/ahmed/events.json'))
#finding the item with the highest sale
event=max(data, key=lambda ev: ev['total_sales'])
print (event)
#sum the "car_sales" of all the items in the json file
count = sum(map(lambda x: int(x['total_sales']),data))
print (count)
这是 json 文件(一个测试)
[
{
"id": 47,
"car": {
"car_make": "Lamborghini",
"car_model": "Murciélago",
"car_year": 2002
},
"price": "$13724.05",
"total_sales": 149
},
{
"id": 48,
"car": {
"car_make": "volvo",
"car_model": "x20",
"car_year": 2010
},
"price": "$13724.05",
"total_sales": 10
},
{
"id": 49,
"car": {
"car_make": "kia",
"car_model": "kia1.2",
"car_year": 2007
},
"price": "$13724.05",
"total_sales": 114
},
{
"id": 50,
"car": {
"car_make": "renault",
"car_model": "p300",
"car_year": 2002
},
"price": "$13724.05",
"total_sales": 147
},
{
"id": 51,
"car": {
"car_make": "ferrari",
"car_model": "red",
"car_year": 2007
},
"price": "$13724.05",
"total_sales": 150
}
]