For example, I have one list as below:
l1 = [1, 2, 3, 4]
I have another list lx
to compare with l1
and need to decide whether lx
matches l1
. A successful match is defined as below:
For each element
e
inlx
,e
must be inl1
too; and for any two sequential elementse1
ande2
, ife1
appears beforee2
, thene1
must also appear beforee2
inl1
.
Some examples are below and their match status with l1 is provided:
l2 = [2, 1] # fails
l3 = [1, 2, 4] # succeeds
l4 = [2, 3] # succeeds
l5 = [2, 3, 1] # fails
l6 = [1,2,3,4,5] # fails
UPDATE for more complicated cases:
l1 = [1, 2, 3, 4]
l2 = [{2}, {1}]
l2 = [{2,1}, {1,3}]
l1 doesn't change, but in the other list, it's always a set of lists. For this example:
l2 = [{2,1}, {1,3}]
It can be expanded into:
l2 = [[2,1], [2,3], [1,1], [1,3]]
which means as long as one of the 4 lists in l2 matches l1, it is a successful match. Since [1,3] matches l1=[1,2,3,4], the whole l2 can match.
So one solution may be first flatten the l2 into the new l2, and then the all(e in i for e in b) can be used.