如何检查一个列表是否在另一个具有相同顺序的python列表中 [英] How to check if a list is in another list with the same order python
本文介绍了如何检查一个列表是否在另一个具有相同顺序的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
这篇关于如何检查一个列表是否在另一个具有相同顺序的python列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文