[Tutor] Replacing a value in a list

nzbz xx nzbzxx at gmail.com
Fri Aug 13 03:14:03 EDT 2021


I came across this problem: Given that -999 is a missing value in a
dataset, replace this value with the mean of its adjacent values. For
example [1, 10, -999, 4, 5] should yield 7.

This is what i got so far. The problem is that i don't know what to do when
there are consecutive -999 in the list e.g.  [1, 10, -999, -999, -999]

def clean_data(case):
    data_cleaned_count = 0
    for data in range(len(case)-1):
        if case[data] == -999:
            ave = (case[data-1] + case[data+1])/2
            case[data] = ave
            data_cleaned_count =  data_cleaned_count + 1
    print(case, data_cleaned_count)


More information about the Tutor mailing list