简体   繁体   English

在python中计算json值平均值

[英]calculate json values average in python

how can I calculate in python the values JSON file in the following example:如何在 python 中计算以下示例中的值 JSON 文件:

"items": [
         {
             "start": "0.6",
             "end": "0.9",
             "alter": [
                 {
                     "conf": "0.6",
                     "content": ""
                 }
             ],
             "type": "pron"
         },
     ]
import json

with open("./file.json") as f:
    dict_data = json.load(f)  # passing file object and will return json in dictionary datatype
confidences = [float(i['alternatives'][0]['confidence']) for i in dict_data['items']]
confidence_avg = sum(confidences) / len(confidences)
print(confidence_avg)

Output:输出:

0.8534666666666667 0.8534666666666667

For starters, your JSON file is missing the first and last curly brackets, so I've added them manually.对于初学者,您的 JSON 文件缺少第一个和最后一个大括号,因此我手动添加了它们。 Without them, it is not valid JSON.没有它们,它就不是有效的 JSON。

Use json.loads to parse the JSON string and return a dict .使用json.loads解析 JSON 字符串并返回一个dict

The confidence values are stored as strings, so they need to be transformed to float s. confidence值存储为字符串,因此需要将它们转换为float

Add them one by one and divide by the number of confidence values.将它们一一相加,然后除以置信度值的数量。 In this case we assume each item has only 1.在这种情况下,我们假设每个项目只有 1 个。

import json

json_str = r"""{
    "items": [
        {
            "start_time": "0.0",
            "end_time": "0.46",
            "alternatives": [
                {
                    "confidence": "0.9534",
                    "content": "رسالة"
                }
            ],
            "type": "pronunciation"
        },
        {
            "start_time": "0.46",
            "end_time": "0.69",
            "alternatives": [
                {
                    "confidence": "0.6475",
                    "content": "اللغة"
                }
            ],
            "type": "pronunciation"
        },
        {
            "start_time": "0.69",
            "end_time": "1.23",
            "alternatives": [
                {
                    "confidence": "0.9595",
                    "content": "العربية"
                }
            ],
            "type": "pronunciation"
        }
    ]
}"""

items = json.loads(json_str)["items"]

average = 0
for item in items:
    confidence = float(item["alternatives"][0]["confidence"])
    average += confidence

average /= len(items)

print(average)

Output:输出:

0.8534666666666667

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM