cumulative addition in a list based on an indices list

  • A+
Category:Languages

Say I have list list_a = [100, 5, 1, 2, 200, 3, 1, 300, 6, 6]

and and another list ind_list = [0, 4, 7]

I want to create a third list that will contain the cumulative sum of the first list which "resets" on every index from ind_list.

To clarify, the result should be res_list = [100, 105, 106, 108, 200, 203, 204, 300, 306, 312]

Any help would be appreciated.

 


how about:

cs= np.cumsum(list_a) for i in ind_list:     if i==0:         continue     cs[i:]-=cs[i-1] 

result:

cs >>array([100, 105, 106, 108, 200, 203, 204, 300, 306, 312]) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: