Skip to content
Advertisement

Compare current element in one list to next element in another list

I have two equally long lists representing the start and end frames of an event. They look something like this:

START     END
111        113
118        133
145        186

The next element of the START list is always going to be bigger than the previous element in the END lists a.k.a 118 is always going to be bigger than 113. I am trying to calculate the difference between the next element of the START list to the previous element in the END list a.k.a 118-113. I want to do this for every element, however I am having difficulties accessing the elements for the proper calculation. Here is what I have so far:

def estimateAverageBlinkInterval(blinkStartFrame, blinkStopFrame):
    
    estimateAverage = []
    
    for i in range(0, len(blinkStartFrame)):
        print(blinkStartFrame)
        for j in range(0, len(blinkStopFrame)):
            print(blinkStopFrame[j])
            estimateAverage.append(blinkStartFrame[i+1]-blinkStopFrame[j])
        
    return mean(estimateAverage) 

I am essentially iterating over both lists, I tried ‘blinkStartFrame[:-1]’ at the for loop but that doesn’t do anything and with the current [i+1] I am getting an ‘IndexError: list index out of range’ which makes sense as i am trying to access and element the loop hasn’t iterated over yet. Any suggestions are more than welcome, thank you!

Advertisement

Answer

Is this that you’re looking for ? You can compare start[i] with end[i-1] ?

start = [111,118, 145]
end = [113, 133, 186]

for i in range(1, len(start)):
    print(start[i] - end[i-1])
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement