How to calculate moving average in Python 3?

后端 未结 5 1669
挽巷
挽巷 2020-12-06 02:21

Let\'s say I have a list:

y = [\'1\', \'2\', \'3\', \'4\',\'5\',\'6\',\'7\',\'8\',\'9\',\'10\']

I want to create a function that calculates

5条回答
  •  天涯浪人
    2020-12-06 02:31

    Use the sum and map functions.

    print(sum(map(int, x[num-n:num])))
    

    The map function in Python 3 is basically a lazy version of this:

    [int(i) for i in x[num-n:num]]
    

    I'm sure you can guess what the sum function does.

提交回复
热议问题