Python获取元组列表中每小时的平均值

vxqlmq5t  于 2022-12-24  发布在  Python
关注(0)|答案(1)|浏览(135)

我想获得每小时每个值的平均值,然后将一天中的每个小时相加。
救命啊!
我在json文件中有一个元组列表,如下所示:

{
    "name1": [
        [
            "2022-12-01T05:02:34Z",
            9.08
        ],
        [
            "2022-12-01T05:24:11Z",
            9.19
        ],
        [
            "2022-12-01T06:02:08Z",
            16.6
        ],
        [
            "2022-12-01T06:49:34Z",
            9.02
        ]
    ],
    "name2": [
        [
            "2022-12-01T05:02:34Z",
            0.512
        ],
        [
            "2022-12-01T05:24:11Z",
            11.77
        ],
        [
            "2022-12-01T06:02:08Z",
            13.61
        ],
        [
            "2022-12-01T06:49:34Z",
            12.25
        ]
    ]
}

我想要这样的东西:

{
"name1": [
    [
        "2022-12-01T05:00:00Z",
        9.135
    ],
    [
        "2022-12-01T06:00:00Z",
        12.81
    ],
    [
        "2022-12-01T07:00:00Z",
        9.025
    ]
],
"name2": [
    [
        "2022-12-01T05:00:00Z",
        6.141
    ],
    [
        "2022-12-01T06:00:00Z",
        12.93
    ],
    [
        "2022-12-01T07:00:00Z",
        20.86
    ]
]

存储在json文件中的数据每隔几分钟记录一次,全年记录一次。

5kgi1eie

5kgi1eie1#

我很想帮忙,但我不确定数据收集的频率。如果你能提供更多信息,我可以修改我的解决方案,我在这里测试了一个伪代码:

import json

# Opening JSON file
with open("data.json") as f:
    # returns JSON object as a dictionary
    data = json.load(f)

sum_value = 0
for message in data["name1"]:
    sum_value += message[1]
avg_value = sum_value / len(data["name1"])

print(sum_value)
print(avg_value)

祝你好运!如果你有任何问题就告诉我。

相关问题