Cumulative summation of a numpy array by index

后端 未结 5 659
北海茫月
北海茫月 2021-02-08 11:54

Assume you have an array of values that will need to be summed together

d = [1,1,1,1,1]

and a second array specifying which elements need to be

5条回答
  •  天命终不由人
    2021-02-08 12:37

    def zeros(ilen):
     r = []
     for i in range(0,ilen):
         r.append(0)
    
    i_list = [0,0,1,2,2]
    d = [1,1,1,1,1]
    result = zeros(max(i_list)+1)
    
    for index in i_list:
      result[index]+=d[index]
    
    print result
    

提交回复
热议问题