Sum slices of consecutive values in a NumPy array

前端 未结 4 1672
甜味超标
甜味超标 2020-12-19 04:16

Let\'s say I have a numpy array a containing 10 values. Just an example situation here, although I would like to repeat the same for an array with length 100.

4条回答
  •  余生分开走
    2020-12-19 04:40

    You could use

    import numpy as np
    
    a = np.array([1,2,3,4,5,6,7,8,9,10])
    step = 5
    
    for i in range(0,a.shape[0],step):
        print(np.sum(a[i:i+step]))
    

提交回复
热议问题