Existing?
As part of a larger project, I thought I might need to search for a sub-list within a given list, and because I am lazy i did a quick google and did not like the answers I found.I started with the thought that the best algorithm for me would be to start searching from the index of the first item in the sublist and so on, but none of the googled answers used list.index.
I decided then to create my own
My version
Well I want to use list.index. If the item is not in the list then it raises an error, so I'll need a try-except block too.
I look for successive first item from the sub-list in the list and if found, accumulate the index in the answer and move on to search for the next match.
It seemed easy to add flags to:
- Stop after finding a first index of the sub-list in the list.
- Allow overlapping matches or not. [1,0,1] is found twice in [1,0,1,0,1] at indices 0 and 2, but only once if overlapping is not allowed
#!/bin/env python3
#%%
from typing import Any
"""
Find instance of a sub-list in a list
"""
defindex_sublist(lst: list[Any],
sublst: list[Any],
only_first: bool=False,
non_overlapping=False,
) -> list[int]:
"Find instance of a (non-empty), sub-list in a list"
ifnot sublst:
raiseValueError("Empty sub-list")
ifnot lst:
return []
first, ln = sublst[0], len(sublst)
ans, i = [], 0
whileTrue:
try:
i = lst.index(first, i)
exceptValueError:
break
if lst[i: i+ln] == sublst:
ans.append(i)
if only_first:
break
i += ln if non_overlapping else1
return ans
#%%
deftest():
assert index_sublist([], [1], only_first=False) == []
assert index_sublist([1], [1], only_first=False) == [0]
assert index_sublist([1,0,1], [1], only_first=False) == [0, 2]
assert index_sublist([2,1,0,1], [1], only_first=True) == [1]
assert index_sublist([2,1,0,1], [1, 3], only_first=False) == []
assert index_sublist([1,0,1,0,1], [1,0,1],
only_first=False,
non_overlapping=False) == [0, 2]
assert index_sublist([1,0,1,0,1], [1,0,1],
only_first=False,
non_overlapping=True) == [0]
#%%
if__name__=='__main__':
test()
End.