1
我想在下面的函数中计算名为'temp_data'的列表的移动平均值。移动平均数据应存储在名为'移动平均'的列表中。下面的代码工作的意思是列表'temp_mov'打印在函数内部(第12行),但不是当我稍后调用函数时(在代码的最后一行)。在那种情况下,我得到一个空的列表。我犯了什么错误?如何制作计算列表移动平均的函数?
# calculate moving average of a list of weather data
def make_moving(temps, temp_mov):
''' Create moving average from list weather data'''
cumsum, temp_mov = [0], []
for i, x in enumerate(temps, 1):
cumsum.append(cumsum[i-1] + x)
if i>=N:
moving_ave = round((cumsum[i] - cumsum[i-N])/N, 1)
temp_mov.append(moving_ave)
print(temp_mov)
return temp_mov
make_moving(temp_data, moving_average)
print(moving_average)