First we define list1:
>>> list1='a,b,c,d,e,f,g,h,i,j'.split(',')
>>> list1
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
As long as yours list1is in alphabetical order, we will not allow this. This code works independently.
Now create list2that failed:
>>> list2 = 'a,b,f,d,e,g,c,h,i,j'.split(',')
>>> list2
['a', 'b', 'f', 'd', 'e', 'g', 'c', 'h', 'i', 'j']
Here's how to check if it works list2:
>>> list2 == sorted(list2, key=lambda c: list1.index(c))
False
False means out of order.
Here is an example that is fine:
>>> list2 = 'a,b,d,e'.split(',')
>>> list2 == sorted(list2, key=lambda c: list1.index(c))
True
True means order is ok.
Ignoring list items1 not in list2
Consider a list2, which has an element not in list1:
>>> list2 = 'a,b,d,d,e,z'.split(',')
To ignore an unwanted element, create list2b:
>>> list2b = [c for c in list2 if c in list1]
:
>>> list2b == sorted(list2b, key=lambda c: list1.index(c))
True
sorted
>>> list2b = ['a', 'b', 'd', 'd', 'e']
>>> indices = [list1.index(c) for c in list2b]
>>> all(c <= indices[i+1] for i, c in enumerate(indices[:-1]))
True