Search code examples
pythonarraysfor-loopmultidimensional-arraycumsum

How to do cumulative sum of array in 3 dimension by python? (for loop in 3 dimension)


I have an array of three dimension

   x=
[[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]],
 [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]],
 [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]],
 [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]]

And I need cumulative sum like the following

   y=
[[[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
 [[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
 [[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
 [[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]]]

And I want to achieve this by using for loop and sum alone, i.e. without using any specific functions like in numpy or itertools, nor any extra temp variables.

I have tried

for k in range(0,1):
    for j in range(0,5):
        for i in range(0,4):
            y[i][j][k]=sum(sum(x[i][j][k] for jj in range(0,5) if jj<=j)for kk in range(0,1) if kk<=k)

but I got

   y[i][j][k]=[[[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]],
 [[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]],
 [[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]],
 [[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]]]

How to do for loop as per my need?

I have

x[0][0][0]=1
x[0][1][0]=2
x[0][2][0]=3
x[0][3][0]=4
x[0][4][0]=5
x[0][0][1]=6
x[0][1][1]=7
x[0][2][1]=8
x[0][3][1]=9
x[0][4][1]=10

. . . . I need to do

y[0][0][0]=x[0][0][0]=1
y[0][1][0]=x[0][0][0]+x[0][1][0]=3
y[0][2][0]=x[0][0][0]+x[0][1][0]+x[0][2][0]=6
y[0][3][0]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]=10
y[0][4][0]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]=15


y[0][0][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]=21
y[0][1][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]=28
y[0][2][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]+x[0][2][1]=36
y[0][3][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]+x[0][2][1]+x[0][3][1]=45
y[0][4][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]+x[0][2][1]+x[0][3][1]+x[0][4][1]=55

. . .

(Reposting my question with my exact requirements clearly)


Solution

  • I believe the usual cumsum functions would put 1, not 21, in y[0][0][1] etc. This code would do so:

    y=[[[0,0] for b in range(5)] for a in range(4)]
    for a in range(4):
        for c in range(2):
            cum = 0
            for b in range(5):
                cum+=x[a][b][c]
                y[a][b][c] = cum
    

    But if you want to get 21 simply move the cum = 0 line outside the c loop

    EDIT

    To avoid the extra variable:

    for a in range(4):
        for c in range(2):
            y[a][0][c] = x[a][0][c]
            for b in range(1,5):
                y[a][b][c] = x[a][b][c] + y[a][b-1][c]
    

    2nd EDIT

    And to get 21 instead of 1 as the second value of the first pair:

    for a in range(4):
        for c in range(2):
            y[a][0][c] = x[a][0][c]
            if c==1:
                y[a][0][1] += y[a][4][0]
            for b in range(1,5):
                y[a][b][c] = x[a][b][c] + y[a][b-1][c]