Or D
Or D

Reputation: 151

Index changes from zero in python array

I have a long biopac file that I was able to import using bioread (python package). The array consist of more than 1 million integers. I need to extract time point from the array. Basically, when the object is changed from zero (0) I should take the index of that point (the array's index is the time in milliseconds). Then, when its back to zero I should also take this time point. I have tried a nested if's without success. it looked like that:

for i,v enumerate(array):
    if v != 0:
        time.append(i/1000)
        continue
        if v==0:
            time_offset.append(i/1000)

Anyone has any idea?

Upvotes: 0

Views: 86

Answers (1)

Barmar
Barmar

Reputation: 781210

You need a state variable to keep track of whether you're looking for zero or non-zero.

time.append(0)
look_for_zero = array[0] != 0
for i, v in enumerate(v[1:]):
    if look_for_zero and v == 0:
        look_for_zero = False
        time.append(i/1000)
    elif not look_for_zero and v != 0:
        look_for_zero = True
        time.append(i/1000)

Upvotes: 2

Related Questions