如何检查一个列表是否在另一个具有相同顺序的python列表中
问题描述:
我的问题与典型问题不同.假设我们有
My question is different with the typical one. Assume we have,
X = ['123', '456', '789']
而且,我们要查找另一个列表是否在X中具有完全相同的顺序.例如,
And, we want to find whether another list is in X with exact same order. For example,
A = ['123', '456']
# should return True since A in X with same order
B = ['456', '123']
# should return False since elements in B are not in same order with X
C = ['123', '789']
# should return False since elements in C are not adjacent in X
有人可以给我任何想法吗?
Can anyone give me any idea?
答
Python 2:
def is_a_in_x(A, X):
for i in xrange(len(X) - len(A) + 1):
if A == X[i:i+len(A)]: return True
return False
Python 3:
def is_a_in_x(A, X):
for i in range(len(X) - len(A) + 1):
if A == X[i:i+len(A)]: return True
return False