首先,我会质疑两个假设:
无论如何,这是一个使用列表的非常天真的方法。它满足您的需求。性能可能会成为问题,具体取决于您需要存储的先前数据点的数量。
此外,您可能没有想到这一点,但是您是否需要过去数据的完整记录?或者你可以放弃东西?
data = [] new_observation = (timestamp, value) # new data comes in data.append(new_observation) # Slice the data to get the value of the last logged datapoint. data[-1] # Slice the data to get the mean of the datapoints for the last n seconds. mean(map(lambda x: x[1], filter(lambda o: current_time - o[0] < n, data))) # Perform a regression on the last n data points to get g/s. regression_function(data[-n:]) # Remove from the log data points older than n seconds. data = filter(lambda o: current_time - o[0] < n, data)