Search code examples
pythonfor-in-loop

Calculate result according to previous item in the list result


I´m running into some trouble making a calculation over a list of numbers. The initial list corresponds to temperature data and I need to calculate the RunningMean temperature each day. The Running Mean for each day depends on the previous day´s running mean as well as the previous day´s temperature (and a constant).

Considering that I have a list with the Temperature, I´m defining the list RunningMean as a copy, and then replacing each item according to the formula.

Temperature = [14.5, 13, 15, 16]
RunningMean = Temperature
RMConstant = 0.8
for i in RunningMean [1:3]:         
    RunningMean [i] = (1-RMConstant)*Temperature[i-1]+RMConstant*RMean[i-1]
#I skip the first item to avoid problems when calling i-1 index
#Replace each item in RunningMean with the result of the formula

For some reason, the iteration at line 5 is giving me the following error: expected index value, got float

Any ideas of what is going wrong? Probably it is a basic mistake, but I´ve been scratching my head for a couple of days already.

Many thanks!


Solution

  • Temperature = [14.5, 13, 15, 16]
    RunningMean = Temperature
    RMConstant = 0.8
    for i in range(1,len(RunningMean)):
        RunningMean [i] = (1-RMConstant)*Temperature[i-1]+RMConstant*RunningMean[i-1]
    

    I didn't know what RMean was so I used RunningMean instead to see results while trying it.