I'm trying find the index of the first element bigger than a threshold, like this:
index = 0
while timeStamps[index] < self.stopCount and index < len(timeStamps):
index += 1
Can this be done in a one-liner? I found:
index = next((x for x in timeStamps if x <= self.stopCount), 0)
I'm not sure what this expression does and it seems to return 0 always... Could somebody point out the error and explain the expression?
Another option is to use np.argmax (see this post for details). So your code would become something like
(timeStamps > self.stopCount).argmax()
the caveat is that if the condition is never satisfied the argmax will return 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With