public
Created

List moving average

  • Download Gist
list_ma.py
Python
1 2 3 4 5 6 7 8 9 10 11
def moving_average(data_set, period=3):
avgs = []
 
for idx, item in enumerate(data_set[period-1:]):
avgs.append(round(sum(data_set[idx:period+idx]) / float(period), 2))
 
return avgs
 
data = [1, 2, 3, 6, 9, 12, 20, 28, 30, 25, 22, 20, 15, 12, 10]
 
assert moving_average(data, 3) == [2.0, 3.67, 6.0, 9.0, 13.67, 20.0, 26.0, 27.67, 25.67, 22.33, 19.0, 15.67, 12.33]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.