Home > Software engineering >  Segmenting a list of data; python
Segmenting a list of data; python

Time:07-10

I am trying to write a code that takes a list flow_rate, changes it into a segmented list list_segmented of length segment_len. Then with that segmented list, I take each index and make it a list of data_segment.

I am getting stuck trying to figure out how to make each list_segmented[i] = data_segment. The last part of the code calls another function for data_segment in which I have previously written and can import it.

Appreciate your help.

def flow_rate_to_disorder_status(flow_rate,segment_len,interval,threshold):
    
    inlist = flow_rate[:]
    list_segmented = []
    disorder_status = []
    
    while inlist:
        list_segmented.append(inlist[0 : segment_len])
        inlist[0 : segment_len] = []
    for i in range(0, len(list_segmented)):
        data_segment = list_segmented[i]
            
    condition = sym.has_symptom(data_segment, interval, threshold)

    disorder_status.append(condition)

CodePudding user response:

You nearly did it:

for i in range(0, len(data_segment)):   # <-- looping thru data_segment
    # data_segment = list_segmented[i]   <--  this was back to front.
    list_segmented[i] = data_segment   # <--  this will work

note: there are cleaner ways of doing this in python (like list comprehension).

Anyway, good question. Hope that helps.

CodePudding user response:

It looks like the lines

condition = sym.has_symptom(data_segment, interval, threshold)
disorder_status.append(condition)

should each be indented by one more level to be inside the for loop, so that they are executed for each data segment.

You presumably also want to return disorder_status at the end of the function.

  • Related