1
私は以下の関数で 'temp_data'というリストの移動平均を計算しようとしています。移動平均データは、「moving_average」というリストに格納する必要があります。以下のコードは、 '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)