import Numpy as np
a = np.array([[5, 1, 8, 1, 6, 1, 3, 2],[2, 3, 4, 1, 6, 1, 4, 2]])
n = 2
[(a[0:2, i:i+n]).sum(axis=1) for i in range(0,a.shape[1],n)]
The output is:
[array([6, 5]), array([9, 5]), array([7, 7]), array([5, 6])]
How can I get a 2D array instead of 4 arrays I got in above output...Is there a better more elegant way doing this using reshape?
source https://stackoverflow.com/questions/75114420/python-summing-a-2d-array-in-steps-defined-with-element-number-range
Comments
Post a Comment