Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据时间戳值计算子列表的平均值_Python_List_Average_Sublist - Fatal编程技术网

Python 如何根据时间戳值计算子列表的平均值

Python 如何根据时间戳值计算子列表的平均值,python,list,average,sublist,Python,List,Average,Sublist,我有两张清单。第一个列表是测量数据时的时间戳(秒)。第二个列表包含数据 我想每10秒计算一次数据的平均值。请注意,两个连续数据点之间的时间戳不是固定的 例如: Timestamp = [2, 5, 8, 11, 18, 23, 25, 28] Data = [1, 2, 3, 4, 5, 6, 7, 8] 预期输出应该是: Output = [average of [1,2,3] , average of [4,5] , average of [6,7,8]] 我想知道Python中是否有类

我有两张清单。第一个列表是测量数据时的时间戳(秒)。第二个列表包含数据

我想每10秒计算一次数据的平均值。请注意,两个连续数据点之间的时间戳不是固定的

例如:

Timestamp = [2, 5, 8, 11, 18, 23, 25, 28]
Data = [1, 2, 3, 4, 5, 6, 7, 8]
预期输出应该是:

Output = [average of [1,2,3] , average of [4,5] , average of [6,7,8]]
我想知道Python中是否有类似average if analysis的内置函数可以自动完成这项工作


谢谢您的帮助。

您可以使用数学函数
floor
defaultdict
作为

from collections import defaultdict
from math import floor

timestamp = [2, 5, 8, 11, 18, 23, 25, 28]
data = [1, 2, 3, 4, 5, 6, 7, 8]
average_dc= defaultdict(list)
for t, d in sorted(zip(timestamp, data), key=lambda x : x[0]):
     average_dc[math.floor(t / 10)].append(d)
averages = [sum(i)/ len(i) for i in average_dc.values()]
输出

[2.0, 4.5, 7.0]
排序(zip(timestamp,data),key=lambda x:x[0])
时间戳
值与
数据
中的值合并在同一索引上,然后for循环将根据相关的
时间戳
值插入相关的
数据
值。
在最后一行中,列表理解将在
average\u dc
中迭代每个列表,并计算其平均值