Loop in python for different steps size

Viewed 50

I do some program which will calculate numbers of values higher than some threshold (Th) but only after it is 3 in line and till the value is less than Th but also 3 in line. In my logic after the program detects 3 values > Th it starts to calculate till 3 values will not be less than Th after that i=k and a new loop start from a number, not i++ but from i = i+k, my program is not working right. Do you have any idea why? I need numbers of values > Th but if it is some single fluctuating it shouldn't be calculated. Maybe you can understand better if you look at the picture. For me is an interesting number of values beetwin these 3 solid markers.
enter image description here

import numpy as np

a = np.array([10, 2, 9, 4, 5, 6, 7, 0, 5, 6, 7, 0, 0, 0, 0, 5, 6, 0, 5, 6, 7])
b = np.array([0])

Th = 1
k = 0
i = 0
for i in range(len(a) - 3):

    if a[i] and a[i + 1] and a[i + 2] > Th:

        while a[i] or a[i + 1] or a[i + 2] > Th:
            k += 1
            print("a: ", a[i])
            print("k: ", k)
            i += 1
    i = i + k
0 Answers
Related