how can i do math operation on list elements in python?

后端 未结 5 1872
北恋
北恋 2021-01-28 03:36

Suppose i have list of numbers [3, 51, 34]. I want to add to each element the sum of the previous elements and return a new list with these new values. So here the

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-28 04:05

    numpy.cumsum might be good choice for something like this.

    In [1]: import numpy as np
    
    In [2]: a = [3,51,34]
    
    In [3]: np.cumsum(a)
    Out[3]: array([ 3, 54, 88])
    

提交回复
热议问题