How can I access the leaf node of the JSON file to retrieve the sales_value of the year 2022 for all customers, and then sum them up in Python?

huangapple go评论70阅读模式
英文:

How can I access the leaf node of the JSON file to retrieve the sales_value of the year 2022 for all customers, and then sum them up in Python?

问题

{
"customer_data": [
{
"name": "John Smith",
"age": 35,
"email": "johnsmith@gmail.com",
"phone": "0413-535-124",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Jane Doe",
"age": 28,
"email": "janedoe@yahoo.com",
"phone": "0401-655-568",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Bob Johnson",
"age": 42,
"email": "bjohnson@hotmail.com",
"phone": "0433-515-912",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Bob2 Johnson",
"age": 42,
"email": "bjohnson@hotmail.com",
"phone": "0433-515-912",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Bob3 Johnson",
"age": 42,
"email": "bjohnson@hotmail.com",
"phone": "0433-515-912",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
}
]
}

要在Python中计算2022年的销售值,你可以使用以下代码:

# 上面的JSON数据
data = {
    "customer_data": [
        {
            "name": "John Smith",
            "age": 35,
            "email": "johnsmith@gmail.com",
            "phone": "0413-535-124",
            "sales_value": [
                {
                    "year": "2022",
                    "sales_value": 2000
                },
                {
                    "year": "2023",
                    "sales_value": 3000
                }
            ]
        },
        {
            "name": "Jane Doe",
            "age": 28,
            "email": "janedoe@yahoo.com",
            "phone": "0401-655-568",
            "sales_value": [
                {
                    "year": "2022",
                    "sales_value": 2000
                },
                {
                    "year": "2023",
                    "sales_value": 3000
                }
            ]
        },
        {
            "name": "Bob Johnson",
            "age": 42,
            "email": "bjohnson@hotmail.com",
            "phone": "0433-515-912",
            "sales_value": [
                {
                    "year": "2022",
                    "sales_value": 2000
                },
                {
                    "year": "2023",
                    "sales_value": 3000
                }
            ]
        },
        {
            "name": "Bob2 Johnson",
            "age": 42,
            "email": "bjohnson@hotmail.com",
            "phone": "0433-515-912",
            "sales_value": [
                {
                    "year": "2022",
                    "sales_value": 2000
                },
                {
                    "year": "2023",
                    "sales_value": 3000
                }
            ]
        },
        {
            "name": "Bob3 Johnson",
            "age": 42,
            "email": "bjohnson@hotmail.com",
            "phone": "0433-515-912",
            "sales_value": [
                {
                    "year": "2022",
                    "sales_value": 2000
                },
                {
                    "year": "2023",
                    "sales_value": 3000
                }
            ]
        }
    ]
}

# 计算2022年的总销售值
total_sales_2022 = sum(customer["sales_value"][0]["sales_value"] for customer in data["customer_data"])
print(total_sales_2022)

这段代码将遍历所有客户的数据,并将2022年的销售值相加,然后打印出总销售值。

英文:

{
"customer_data": [
{
"name": "John Smith",
"age": 35,
"email": "johnsmith@gmail.com",
"phone": "0413-535-124",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Jane Doe",
"age": 28,
"email": "janedoe@yahoo.com",
"phone": "0401-655-568",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Bob Johnson",
"age": 42,
"email": "bjohnson@hotmail.com",
"phone": "0433-515-912",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Bob2 Johnson",
"age": 42,
"email": "bjohnson@hotmail.com",
"phone": "0433-515-912",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
},
{
"name": "Bob3 Johnson",
"age": 42,
"email": "bjohnson@hotmail.com",
"phone": "0433-515-912",
"sales_value": [
{
"year": "2022",
"sales_value": 2000
},
{
"year": "2023",
"sales_value": 3000
}
]
}
]
}

Using above json file needs to calculate sales_value of the year 2022 in python.
Pls can you guide me how to get leaf node and calculate total sales value of 2022

I have tried to get leaf node

答案1

得分: 1

以下是翻译好的部分:

如果你确信 JSON 中会包含每个客户的 'sales_value' 字段,那么你可以尝试以下代码,它应该是不言自明的:

import json
with open('file.json', 'r') as f:
    data = json.load(f)
total_sales_value = 0
for customer in data['customer_data']:
    for sales in customer['sales_value']:
        if sales['year'] == '2022':
            total_sales_value += sales['sales_value']

至于不太可读的一行代码:

sum([sales['sales_value'] for customer in data['customer_data'] for sales in customer['sales_value'] if sales['year'] == '2022'])

输出:

10000
英文:

If you're certain that the json would contain the 'sales_value' field for each customer, then you can try the follwing, which should be self explanatory:

import json
with open('file.json', 'r') as f:
data = json.load(f)
total_sales_value = 0
for customer in data['customer_data']:
for sales in customer['sales_value']:
if sales['year']=='2022':
total_sales_value += sales['sales_value']

As for less readable one liner:

sum([sales['sales_value'] for customer in data['customer_data'] for sales in customer['sales_value'] if sales['year']=='2022'])

Output:

10000

huangapple
  • 本文由 发表于 2023年6月5日 10:25:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定