So lets say I have a list in python:
[0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1]
and I want to find the indices where 1's start and end, eg:
[[4, 5], [8, 13], [16, 16], [18, 21]]
What is an efficient way to do this in python? All I can think of are expensive for loops where I look forward/backward if the iteration shows a 1 and before it was a 0? Perhaps a cumsum? But that would still require a loop.
Also, would this problem change if I were to add rules? like there must be at least 3 consecutive 1's before its recorded, eg:
[[8, 13], [18, 21]]
or there must be 3 consecutive instances of any digit before a change in pattern is recorded or stopped, eg:
[[8, 21]]
My problem is with lists of characters, but the core of it can be boiled down to this mindset.
My inefficient solution is to scan through with some padding:
answer = []
record_flag = 0
vec = [0] + vec + [0]
for i in range(len(vec)-1):
if vec[i] == 0 and vec[i+1] == 1:
start = i
record_flag += 1
elif vec[i] == 1 and vec[i+1] == 0:
end = i-1
record_flag += 1
if record_flag >= 2:
record_flag = 0
answer.append([start, end])
and if I need to add rules, I'd just manually insert them into the if-statements. But this does not feel very pythonic. Any advice?