我有一个很长的列表,我们称它为y
。 len(y) = 500
。我故意不在代码中包含 y。
对于 y 中的每个项目,我想找到该项目的平均值及其 5 个连续值。当我到达列表中的最后一项时遇到问题,因为我需要对下面的一行使用“a+1”。
a = 0
SMAlist = []
for each_item in y:
if a > 4 and a < ((len(y))-1): # finding my averages begin at 6th item
b = (y[a-5:a+1]) # this line doesn't work for the last item in y
SMAsix = round((sum(b)/6),2)
SMAlist.append(SMAsix)
if a > ((len(y))-2): # this line seems unnecessary. How can I avoid it?
b = (y[-6:-1]+[y[a]]) # Should I just use negative values in general?
SMAsix = round((sum(b)/6),2)
SMAlist.append(SMAsix)
a = a+1
最佳答案
关于@Vivek Kalyanarangan 的“ zipper ”解决方案的一点警告。
对于较长的序列,这很容易失去意义。为了清楚起见,让我们使用 float32
:
>>> y = (1000 + np.sin(np.arange(1000000))).astype(np.float32)
>>> window=6
>>>
# naive zipper solution
>>> s=np.insert(np.cumsum(np.array(y)), 0, [0])
>>> output = (s[window :] - s[:-window]) * (1. / window)
# towards the end the result is clearly wrong
>>> print(output[-10:])
[1024. 1024. 1024. 1024. 1024. 1024. 1024. 1024. 1024. 1024.]
>>>
# this can be alleviated by first taking the difference and then summing
>>> np.cumsum(np.r_[y[:window].sum(), y[window:]-y[:-window]])/window
array([1000.02936, 999.98285, 999.9521 , ..., 1000.0247 , 1000.05304,
1000.0367 ], dtype=float32)
>>>
# compare to last value calculated directly for reference
>>> np.mean(y[-6:])
1000.03217
为了进一步减少错误,可以对 y
进行分块,并在不损失太多速度的情况下每隔如此多的术语锚定 cumsum。
关于python - 如何创建一个包含最后一个元素的子列表,但对所有其他相同大小的子列表使用通用公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48879615/