python 在每个索引处对数组中的所有先前值求和

标签 python python-2.7 numpy sum

我有一个数组:

my_array = [1, 4, 1, 13, 9]

并想创建一个新数组,my_array 中的每个索引是所有先前索引值的总和

summed_array = [0, 1, 5, 6, 19]

我试过类似的东西

for ind,i in enumerate(my_array):
    print i, my_array[ind-1]

但无法弄清楚如何获得所有先前值的总和。

最佳答案

>>> from numpy import cumsum, ones
>>> a = ones(10)
>>> print(cumsum(a))
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

关于python 在每个索引处对数组中的所有先前值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883758/

相关文章:

python-2.7 - Python (Win 10) : Installing matplotlib requires packages "freetype" and "png"?

python - S3 : ExpiredToken error for S3 pre-signed url within expiry period

Python 从命令行读取文件并用非常大的文件剥离 "\n\r"

list - 预期为 'collections.Iterable' ,改为 Optional[list]

python - 超集 : Where to force an autologin

python - 将列表转换为单独的单独列表

python - 如何在 Python sklearn 中修复 SVR 图

python - 将元组的结果添加到多个字符串

python - 将文件加载到 2d numpy 数组中的有效方法

python - Matplotlib 直方图 - 绘制大于给定值的值