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

Viewed 62

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!

5 Answers

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])

Concept

Loop for n-1 rounds where n is the length of the arrays (assume both have equal length). In each loop, find the difference between i+1 (next start) and i (previous end), and put the result into an array diff. That's it. If you want mean, then just sum it up and divide it with its length.

Code

start = [111,118,145]
end = [113,133,186]
diff = []
for i in range(len(start)-1):
    diff.append(start[i+1]-end[i])
print(diff)
mean = sum(diff)/len(diff)
print(mean)

Your idea is correct. You just need to make it more simpler and handle the exceptions.

  1. You don't need two loops to solve it. -> only one loop is enough.
  2. You will have the error IndexError, because blinkStopFrame[-1] is not valid. -> need to except the index 0
  3. You will also have the error IndexError too, if the two arrays don't have the same length. -> need to check the length of second array

By the way, you can try this to fill the estimateAverage.

for index, value in enumerate(blinkStartFrame):
    if index == 0:
        continue
    if (index - 1) >= len(blinkStopFrame):
        break
    estimateAverage.append(value - blinkStopFrame[index -1])

If both START and END are numpy arrays and have equal length then:

est = START[1:] - END[:-1]
mean_est = np.mean(est)
def estimateAverageBlinkInterval(blinkStartFrame, blinkStopFrame):
    
    estimateAverage = [start-stop for start, stop in zip(blinkStopFrame[1:], blinkStartFrame[:-1])]
    return sum(estimateAverage)/len(estimateAverage)


start = [111, 118, 145]
end = [113, 133, 186]
print(estimateAverageBlinkInterval(start, end))
Related