This should work:
main_list = [True, True, False, False, True, True, True, True, True, False]
start_true = -1
last_added = -1
true_index = []
for i, value in enumerate(main_list):
if value:
if start_true == -1:
start_true = i
else:
if start_true != last_added:
true_index.append(start_true)
last_added = start_true
else:
start_true = -1
print(true_index)
Also if you want the code to detect consecutive Trues including a single True here is a version that does that:
main_list = [True, False, False]
start_true = -1
last_added = -1
true_index = []
for i, value in enumerate(main_list):
if value:
if start_true == -1:
start_true = i
if start_true != last_added:
true_index.append(start_true)
last_added = start_true
else:
start_true = -1
print(true_index)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…