I have a python list with binary information having either a sequence of 1s or a sequence of 0s for example:
event = [0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1]
How can I store the index values of the starting 1 and ending 1?
For example:
if event = [0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1] then result = [2,5,8,11]
EDIT2 i realised that result should ideally be result_start=[2,8] result_end=[5,11], like lazyr solution, this way we understand where just a single 1 located. eg. event=[0,0,1,1,0,1], would yield a wrong list for the last 1. Thanks for trying all!
EDIT1: I need the index of the first and the last 1 but first to the last 1.
I tried to code it like the following:
k=False
j=0
result=[]
for i in event:
if i==1:
k=True
result.append(k)
else:
k=False
EDIT:
Also find range that starts at index 0.
Created
one_rangesfunction which returns a list of(start, end)tuples.