Home > Software engineering >  How to fix this failing recursion? (Python, recursion, nested lists)
How to fix this failing recursion? (Python, recursion, nested lists)

Time:07-11

CLARIFICATIONS: Adding some clarification based on the questions asked: The bigger process - my user watches a video, and hits a keyboard key as the video plays. Every time they hit the keyboard, the timestamp (in milliseconds) of the video is being recorded in a list. The video is comprised of many small videos (each one is a trial of about 20-60 seconds) that were concatenated. The computer doesn't know what trials are (for the computer this is just one long video) but the user does know about the trials. I need to divide this list based on the trials' time ranges, but there are not rules here - sum of values should not be anything, one trial can have 6 time stamps, another one can have 38. I just need to divide this big list of values, based on what range of time they represents (= which trial they were part of)

trials_num is there just to know how many possible ranges I have - instead of running infinite amount of times, to run it based on the amount of trials (time ranges) I have. It should eventually translate to the amount of sublists I'll eventually have (as the times within each trial should be written to a separate sublist)

trial_length is there to give me the length of each range of time I need to divide based on - in the example I have here, if my trial length is 5 seconds, which is 5000 ms, my ranges will be 0-5000, 5001-10000, 10001-15000 etc... so, trial length is the size of each range, and trials_num is the number of ranges I have (could be seen as n, and then n*trial_length gives me the range I am in)

*ORIGINAL QUESTION: I am trying to do the following: I have a list of values (floats if it matters, representing ms in a video). I need to divide it based on the values in the list, but I have many ranges, and each range should be a different sublist in the final list.

my code is as follows:

def break_lists(orig_list, trial_length, trials_num, final_list):
    # This section builds a list of the number of sublists I should have, the number of ranges I will work with.
    t = []
    for num in range(1, trials_num   1):
        t.append(num)
    # take the given range and multiply by 1000 (user will give in seconds and my list is in ms)    
    ms_length = trial_length * 1000
    # loop through the number of trials that I have (sublists)
    for num in t:
        # create a new sublist each time
        sublist = []
        # loop through values 0-40 in my list (max values per sublist will be 40, but could be less!)
        for i in range(0, 40):
            # check if value in original list is smaller than the highest border of the range
            if orig_list[i] <= ms_length * num:
                # if it is, add it to the sublist
                sublist.append(orig_list[i])

            else:
                # if it's not, call the function again starting from the last value you got to on the original list
                break_lists(orig_list[i:], trial_length, trials_num, final_list)
        
        final_list.append(sublist)
    print(final_list)
    return(final_list)

When I use this list: list = [2000, 3000, 4000, 5000, 6000, 7000, 9000, 12000, 15000, 17000, 19000, 80000] and call the function: break_lists(list, 5, 20, [])

My goal is to get it to be this final_list = [[2000, 3000, 4000, 5000], [6000, 7000, 9000], [12000, 15000], [17000, 19000], [80000]]

at the moment (and in many other moments along my way) i got the message saying 'maximum recursion depth exceeded in comparison'. I keep playing and changing it to try to figure out the problem, so sometimes I do get a list with my values, but with MANY empty lists between the values from the list (more then the number of trials), I have no idea why

I am sure there are trillion ways to do that, probably way more efficiently than what I got here, but my brain is kinda tired from that and stuck on that recursion idea. I also am new here and don't know about packages or modules that might have an easy solution for my problem. Any ideas will be appreciated! (I also thought of dividing the values while I get them from the user, but i believe it is basically the same and will probably be better to do the separation after, in a separate function, and not within another function)

THANK YOU FOR READING UNTIL HERE AND FOR YOUR HELP!!!

CodePudding user response:

IIUC:

Code

def break_lists(orig_list, trial_length, trials_num):
    result = [[] for _ in range(trials_num)]      # Create ranges
    for v in orig_list:
        bin_ = int(v/(trial_length*1000))        # find bin
        if bin_*trial_length*1000 == v:
            bin_ -= 1                            # bin should include right edge
        result[bin_].append(v)                   # Add data to bin
                   
    return result
                   

Test

print(break_lists(orig_list, 5, 20))

# Output:
[[2000, 3000, 4000, 5000],
 [6000, 7000, 9000],
 [12000, 15000],
 [17000, 19000],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [80000],
 [],
 [],
 [],
 []]
​
  • Related