python - 如何创建一个包含最后一个元素的子列表,但对所有其他相同大小的子列表使用通用公式?

标签 python loops for-loop sublist

我有一个很长的列表,我们称它为ylen(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/

相关文章:

c++ - 为什么我在 C 中使用 openMP 时无法在 for 循环中定义数据类型?

python - 在表单中创建新的外键

python - GCP dataproc with presto - 有没有办法使用 pyhive 通过 python 远程运行查询?

Python 无法将错误的 unicode 编码为 ascii

python - 将两个 .join 方法合二为一

javascript - 在jquery中做一个循环

css - 用 $.each 每隔一行改变颜色

r - 方差分析,for 循环应用函数

android - 以 20 个 kotlin block 的形式迭代字符串的字符

php - Wordpress 帖子类别名称链接