[Python] Calculate cumulative sum from last non-zero entry in python

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I have a numeric series like [0,0,0,0,1,1,1,0,0,1,1,0]. I would like to calculate the numeric sum from the last non-zero values. i.e the cumsum will be reset to zero once a zero entry occurs.

    input: [0,0,0,0,1,1,1,0,0,1,1,0]
    output:[0,0,0,0,1,2,3,0,0,1,2,0]


    Is there a built-in python function able to achieve this? Or better way to calculate it without loop?

    Continue reading...

Compartilhe esta Página