There are three problems to fix:
if dispo[iter]
should be if iter
(# 1)
- empty subsequences should be ignored (# 2)
- if you want the indices of the interval boundaries, fix off-by one error (# 3)
- (bonus)
iter
is not a good variable name, because it shadows the builtin iter
function
dispo = [0,0,1,1,1,1,0,0,0,0,1,1]
Forbidden_intervals = []
pointer = 0
while pointer < len(dispo):
length_inter = 0
for i in dispo[pointer:]: # 1
if i == 0:
length_inter += 1
else:
break
if length_inter: # 2
Forbidden_intervals.append((pointer, pointer+length_inter-1)) # 3
pointer += length_inter + 1
print(Forbidden_intervals)
That all said, it's not the most efficient way to do this, because of quite many redundant sublist operations. I would implement it with something like a simple finite state machine (with just two states):
dispo = [0,0,1,1,1,1,0,0,0,0,1,1]
forbidden = []
# forbidden_start keeps the index of the ongoing zero sequence start
# or -1 if we're not in a zero sequence
forbidden_start = -1
for i, v in enumerate(dispo):
if v:
if forbidden_start >= 0:
forbidden.append((forbidden_start, i-1))
forbidden_start = -1
elif forbidden_start < 0:
forbidden_start = i
# just in case if the list ends with zeros
if forbidden_start >= 0:
forbidden.append((forbidden_start, len(dispo)-1))