简体   繁体   English

python 将数组中每个索引处的所有下一个 n 值相加

[英]python sum all next n values in array at each index

I have an array:我有一个数组:

my_array = [1, 13, 6, 100, 12,23,45] and would like to create a new array that for each index in my_array is the sum of 3 next index values my_array = [1, 13, 6, 100, 12,23,45] 并且想要创建一个新数组,对于 my_array 中的每个索引,它是 3 个下一个索引值的总和

summed_array = [119, 118, 135, 80, 68,45,0] I tried something like np.cumsum but this cumlative values summed_array = [119, 118, 135, 80, 68,45,0] 我尝试了类似 np.cumsum 的东西,但是这个累积值

import numpy as np

sum_value = 0

my_array = [1, 13, 6, 100, 12,23,45]
summed_array = [0, 0, 0, 0, 0,0,0]
print(len(my_array))
for ind,i in enumerate(my_array):
    if ind+3< len(my_array):
        summed_array[ind] =my_array[ind+1]+my_array[ind+2]+my_array[ind+3]
    elif ind+2 < len(my_array):
         summed_array[ind] =my_array[ind+1]+my_array[ind+2]
    elif ind+1 < len(my_array):
        summed_array[ind]=my_array[ind+1]
    else:
        summed_array[ind] = 0
print(summed_array)  ``` 

This should do the trick using slices .这应该使用slices来解决问题。

import numpy as np

sum_value = 0

my_array = [1, 13, 6, 100, 12,23,45]
summed_array = [0, 0, 0, 0, 0,0,0]
n = 3;
print(len(my_array))
for i in range(len(summed_array)):
    summed_array[i] = sum(my_array[i+1:i+1+n])
print(summed_array)

With a being your array: a你的数组:

>>> c = a.cumsum()
>>> np.concatenate((c[3:], [a.sum()] * 3)) - c
array([119, 118, 135,  80,  68,  45,   0])

You can also do it in list comprehension:您也可以在列表理解中执行此操作:

sum_array = [ sum(my_array[pos+1:pos+4]) for pos in range(len(my_array)) ]

This way there is no need for declaring the sum_array , as it will always be already created with the correct size.这样就不需要声明sum_array ,因为它总是已经以正确的大小创建。

Edit: Fixed the 'next 3 values', since I hadn't realised it in the first place.编辑:修复了“下 3 个值”,因为我一开始没有意识到。

If you are trying to minimize the number of additions performed, it might be a good idea to have a pointer so that you add and subtract once for each index instead of adding 3 times.如果您试图最小化执行的加法次数,最好有一个指针,这样您就可以为每个索引加减一次,而不是加 3 次。 You might find a different solution more optimum than this though.不过,您可能会找到比这更优化的不同解决方案。 If so, please let me know:) Let a be your array.如果是这样,请告诉我:) 让a成为你的数组。

# Initialize sum array sa with first index
val = sum(a[1:4])
sa = [val]
i = 1

# Get sum for all except last 3 indices
for i in range(1, len(a) - 3):
  val = val + a[i + 3] - a[i]
  sa.append(val)

# Account for the last 3 indices
while i < len(a):
  val -= a[i]
  sa.append(val)
  i += 1

# sa is the array needed at this point

Please note that empty array would currently return [0].请注意,空数组当前将返回 [0]。 If you intend to return an empty array instead, that would be an edge case that can be handled by an if statement in the beginning如果您打算返回一个空数组,那将是一个边缘情况,可以在开始时由 if 语句处理

This can be done这可以做到

a = my_array
summed_array= [sum(a[i:i+3]) for i in range(len(a))]

暂无
暂无

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

相关问题 python在每个索引处总结数组中的所有先前值 - python sum all previous values in array at each index 如何使用python将所有索引值的总和加到numpy ndarray的每个元素中? - How can I add to each element of a numpy ndarray the sum of all its index values with python ? 将数组除以所有值的总和-Python - Divide an array by the sum of all values - Python 如何在 python 的数组中找到非主索引值的总和? - How to find sum of non-prime index values in an array in python? 根据索引数组求和值 - Sum values according to an index array Python / numpy:最有效的方法是对数组中的n个元素求和,以便每个输出元素是前n个输入元素的总和? - Python/numpy: Most efficient way to sum n elements of an array, so that each output element is the sum of the previous n input elements? 数组中的值总和-Python - Sum of values in array - Python Python Pandas - 如何获得前n个值和所有其他值的总和 - Python Pandas - how to get top n values and the sum of all other values Python:将2d数组的每一行缩放为总和为1的值。每一行包含一些负值 - Python: Scale each row of 2d array to values that sum to 1. Each row contains some negative values 使用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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM