How to remove an element and append python list conditionally?

Question:

I receive timeseries data from a broker and want to implement condition monitoring on this data. I want to analyze the data in a window of size 10. The window size must always stay the same. When the 11th data comes, I need to check its value against two thresholds which are calculated from the 10 values inside a window. If the 11th data is outsider, I must delete the data from the list and if it is within the range, I must delete the first element and add the 11th data to the last element. So this way the size of window stays the same. The code is simplified. data comes each 1 second.

temp_list = []
window_size = 10

if len(temy_list) <= window_size :
    temp_list.append(data)
if len(temp_list) == 10:
    avg = statistics.mean(temp_list)
    std = statistics.stdev(temp_list)
    u_thresh = avg + 3*std
    l_thresh = avg - 3*std
    temp_list.append(data)
    if temp_list[window_size] < l_thresh or temp_list[window_size] > u_thresh:
        temp_list.pop(-1)

    else:
        temp_list.pop(0)
        temp_list.append(data)

With this code the list does not get updated and 11th data is stored and then no new data. I don’t know how to correctly implement it. Sorry, if it is a simple question. I am still not very comfortable with python list. Thank you for your hint/help.

Asked By: Yas Gh

||

Answers:

With how your code currently is if you plan to keep the last data point you add it twice instead. You can simplify your code down to make it a bit more clear and straightforward.

##First setup your initial variables
temp_list = []
window_size = 10

Then –

While(True):
    data = ##Generate/Get data here

    ## If less than 10 data points add them to list
    if len(temp_list) < window_size :
        temp_list.append(data)
    ## If already at 10 check if its within needed range
    else:
        avg = statistics.mean(temp_list)
        std = statistics.stdev(temp_list)
        u_thresh = avg + 3*std
        l_thresh = avg - 3*std
        ## If within range add point to end of list and remove first element
        if(data >= l_thresh and data <= u_thresh):
            temp_list.pop(0)
            temp_list.append(data)
Answered By: Karan Shishoo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.