Issue
first of all I am sorry if this have been answered elsewhere. But I have been trying to solve this issue where I want to get the index where n elements repeats it self. So for example if I wanted the index from a list containing bools where True
have repeated itself 3 times:
A = [False, False, True, True, True, False, True, True, True]
So in this example I would want the index 2.
Solution
Heres another approach.
def get_index(lst, n):
for i in range(len(lst)):
if lst[i:i+n] == [True]*n:
return i
return -1
Usage is basically
print(get_index(lst, 3))
returns index if found, else returns -1.
You could of course modify True
to be any value you want to match against.
Answered By - Martin Namukombo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.