How to find the cumulative sum of numbers in a list?

前端 未结 21 1605
挽巷
挽巷 2020-11-22 02:09
time_interval = [4, 6, 12]

I want to sum up the numbers like [4, 4+6, 4+6+12] in order to get the list t = [4, 10, 22].

21条回答
  •  长情又很酷
    2020-11-22 02:53

    I did a bench-mark of the top two answers with Python 3.4 and I found itertools.accumulate is faster than numpy.cumsum under many circumstances, often much faster. However, as you can see from the comments, this may not always be the case, and it's difficult to exhaustively explore all options. (Feel free to add a comment or edit this post if you have further benchmark results of interest.)

    Some timings...

    For short lists accumulate is about 4 times faster:

    from timeit import timeit
    
    def sum1(l):
        from itertools import accumulate
        return list(accumulate(l))
    
    def sum2(l):
        from numpy import cumsum
        return list(cumsum(l))
    
    l = [1, 2, 3, 4, 5]
    
    timeit(lambda: sum1(l), number=100000)
    # 0.4243644131347537
    timeit(lambda: sum2(l), number=100000)
    # 1.7077815784141421
    

    For longer lists accumulate is about 3 times faster:

    l = [1, 2, 3, 4, 5]*1000
    timeit(lambda: sum1(l), number=100000)
    # 19.174508565105498
    timeit(lambda: sum2(l), number=100000)
    # 61.871223849244416
    

    If the numpy array is not cast to list, accumulate is still about 2 times faster:

    from timeit import timeit
    
    def sum1(l):
        from itertools import accumulate
        return list(accumulate(l))
    
    def sum2(l):
        from numpy import cumsum
        return cumsum(l)
    
    l = [1, 2, 3, 4, 5]*1000
    
    print(timeit(lambda: sum1(l), number=100000))
    # 19.18597290944308
    print(timeit(lambda: sum2(l), number=100000))
    # 37.759664884768426
    

    If you put the imports outside of the two functions and still return a numpy array, accumulate is still nearly 2 times faster:

    from timeit import timeit
    from itertools import accumulate
    from numpy import cumsum
    
    def sum1(l):
        return list(accumulate(l))
    
    def sum2(l):
        return cumsum(l)
    
    l = [1, 2, 3, 4, 5]*1000
    
    timeit(lambda: sum1(l), number=100000)
    # 19.042188624851406
    timeit(lambda: sum2(l), number=100000)
    # 35.17324400227517
    

提交回复
热议问题