简体   繁体   English

使用numpy,如何生成一个数组,其中每个索引的值是从0到第二个数组中相同索引的值的总和?

[英]Using numpy, how can I generate an array where the value at each index is the sum of the values from 0 to that same index in a second array?

Given a numpy array a=[3,5,7] 给定一个numpy数组a = [3,5,7]

How can I efficiently generate a second array b, where b[i] = numpy.Sum(a[0:i]? 如何有效地生成第二个数组b,其中b [i] = numpy.Sum(a [0:i]?

I've looked through the numpy docs but the solution isn't jumping out at me... 我已经查看了numpy文档,但是解决方案并没有对我跳出来...

The expected output would be b=[3,8,15] 预期输出为b = [3,8,15]

Any ideas will be gratefully received!!! 任何想法将不胜感激!!!

Thanks, 谢谢,

Doug 道格

You seem to want the cumsum function from numpy here: 您似乎希望从numpy获得cumsum函数:

a=np.array([3,5,7])

In [1]: np.cumsum(a)
Out[1]: array([ 3,  8, 15], dtype=int32)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用另一个数组中唯一值的索引替换numpy数组中的重复值? - How can I replace recurring values in a numpy array by the index of the unique value from another array? numpy数组的和行,其中每个和的起始索引来自另一个数组 - Sum rows of numpy array where start index of each sum comes from another array 如何沿着 numpy 数组的给定轴索引每个出现的最大值? - How can I index each occurrence of a max value along a given axis of a numpy array? 如何使用索引定义数组中的值作为索引来定义数组中的值 - How can I use values from an index-defining array as the index to define a value in an array 如何返回numpy数组的索引? - How can I return the index of a numpy array? 如何使用另一个 numpy 数组中的索引更新 numpy 数组 - How can I update a numpy array with index in another numpy array 如何使用布尔型方形对称 numpy 数组中的 True 值存储存储索引对? - How can I store store index pairs using True values from a boolean-like square symmetric numpy array? 使用同一数组中的另一个值作为索引替换numpy数组中的值 - Replacing value in a numpy array using as an index another value from the same array 如何使用python将所有索引值的总和加到numpy ndarray的每个元素中? - How can I add to each element of a numpy ndarray the sum of all its index values with python ? 如何在 numpy 创建布尔数组的数组的每一行中搜索索引值 - How do I search for index values in each row of an array in numpy creating a boolean array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM