What is a efficient way to check if a list is within another list? Something like:
[2,3] in [1,2,3,4] #evaluates True
[1,5,4] in [5,1,5,4] #evaluates True
[1,2] in [4,3,2,1] #evaluates False
Order within the list matters.
What is a efficient way to check if a list is within another list? Something like:
[2,3] in [1,2,3,4] #evaluates True
[1,5,4] in [5,1,5,4] #evaluates True
[1,2] in [4,3,2,1] #evaluates False
Order within the list matters.
def check_ordered_sublist_in_list(sub_list, main_list):
sub_list = np.array(sub_list)
main_list = np.array(main_list)
return any(all(main_list[n:(n + len(sub_list))] == sub_list)
for n in range(0, len(main_list) - len(sub_list) + 1))
>>> check_ordered_sublist_in_list([2, 3], [1, 2, 3, 4])
True
>>> check_ordered_sublist_in_list([1, 5, 4], [5, 1, 5, 4])
True
>>> check_ordered_sublist_in_list([1, 2], [4, 3, 2, 1])
False
This converts the lists to numpy arrays (for computational efficiency) and then uses slicing to check if the sub_list
is contained within the slice. Any success returns True.
You could use this:
def is_in(short, long):
return any(short==long[i:i+len(short)] for i in range(len(long)-len(short)+1))
is_in([2,3], [1,2,3,4]) # True
is_in([1,5,4], [5,1,5,4]) # True
is_in([1,2], [4,3,2,1]) # False
If you really care about speed, these expressions are 20-30% faster:
def segments(long, length):
return [long[i:i+length] for i in range(len(long)-length+1)]
def is_in_seg(short, long):
return short in segments(long, len(short))
is_in_seg([1,5,4], [5,1,5,4]) # true
[1,5,4] in segments([5,1,5,4], 3) # true
And this is 47% faster, but it uses tuples instead of lists:
import itertools
def segments_zip(long, length):
return itertools.izip(*[long[i:] for i in xrange(length)])
(2,3) in segments_zip((1,2,3,4), 2) # True
(1,5,4) in segments_zip((5,1,5,4), 3) # True
(1,2) in segments_zip((4,3,2,1), 2) # False
The extra speed comes from using itertools.izip, which can stop generating segments when a match is found; from using xrange, which avoids creating the whole range list; and from using tuples, which are generally slightly faster than lists. But the tiny speed advantage will vanish if you have to convert lists to tuples to use it.