Check If List Is Part Of List Keeping Order And Find Postion
I need to seek the 'matchingpoint' of two list where List 'a' is bigger than List 'b'. So far I've found this earlier post: Check if a list is part of another list while preserving
Solution 1:
You could use next to fetch the first matching point:
a = [3, 4, 1, 2, 4, 1, 5]
b = [4, 1, 2]
starting_point = next((a[i] for i in range(len(a)) if b == a[i:i + len(b)]), -1)
print(starting_point)
Output
4
UPDATE
If you need both the index and the value at the index, return the index instead of the value, for example:
position = next((i for i in range(len(a)) if b == a[i:i + len(b)]), -1)
print("position", position)
print("starting point", a[position])
Output
position 1
starting point 4
Note the change, now is i
instead of a[i]
Post a Comment for "Check If List Is Part Of List Keeping Order And Find Postion"