0

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?

4
  • 3
    Please post your code with correct indentation. It's impossible to tell what you're doing from what you posted. Commented Mar 6, 2019 at 22:57
  • 1
    If you've really nested the second if inside the first one, it will never execute. The continue statement goes to the next iteration of the loop, skipping over the rest of the code in the loop. Commented Mar 6, 2019 at 22:58
  • Sorry for not indenting it correctly. Commented Mar 6, 2019 at 23:10
  • 1
    Don't just apologize, fix it. Commented Mar 6, 2019 at 23:17

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Seems like you have solved the issue! Thank you for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.