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!
CodePudding user response:
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])
CodePudding user response:
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)
CodePudding user response:
Your idea is correct. You just need to make it more simpler and handle the exceptions.
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])
CodePudding user response:
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))