Python 是否有一种有效的方法可以使用另一个列表来搜索维护列表的列表';什么命令?

Python 是否有一种有效的方法可以使用另一个列表来搜索维护列表的列表';什么命令?,python,list,Python,List,我刚刚开始学习python。 我需要用一个列表搜索另一个列表,但我必须保持我正在搜索的列表的顺序。例: MylistA = [A, B, G, S, X] MylistB = [A, G, B] 我希望它返回false,因为ListB与ListA的顺序不同。但是,如果是: ListA =[A, B, G, S, X] ListB =[A, B, G] 我希望它返回True 下面是我尝试过的,但是它占用了很多行,效率低下 MylistA = [A, Q, V, B, G, D, F, R,

我刚刚开始学习python。 我需要用一个列表搜索另一个列表,但我必须保持我正在搜索的列表的顺序。例:

MylistA = [A, B, G, S, X]

MylistB = [A, G, B]
我希望它返回false,因为
ListB
ListA
的顺序不同。但是,如果是:

ListA =[A, B, G, S, X]
ListB =[A, B, G]
我希望它返回
True

下面是我尝试过的,但是它占用了很多行,效率低下

MylistA = [A, Q, V, B, G, D, F, R, T, B, G, S, Q]
MylistB = [B, G, D, F, R, T]

ListFound = 0
Pos1 = 0
Pos2 = 1
Pos3 = 2
Pos4 = 3
Pos5 = 4
Pos6 = 5

Pos1A = 0
Pos2A = 1
Pos3A = 2
Pos4A = 3
Pos5A = 4
Pos6A = 5

while Pos6 <= len(MylistA):
    if MylistA[pos1] == MylistB[Pos1A] and \
            MylistA[pos2] == MylistB[Pos2A] and \
            MylistA[pos3] == MylistB[Pos3A] and \
            MylistA[pos4] == MylistB[Pos4A] and \
            MylistA[pos5] == MylistB[Pos5A] and \
            MylistA[pos6] == MylistB[Pos6A]:
        print("MylistB found within MylistA at positions", Pos1, Pos2, Pos3, Pos4,     
               Pos5, Pos6)
        MylistFound += 1
    elif Pos6 >= len(ListA):
        print("MylistB was found", ListFound, "times within MylistA") 
    Pos1 += 1
    Pos2 += 1
    Pos3 += 1
    Pos4 += 1
    Pos5 += 1
    Pos6 += 1
MylistA=[A,Q,V,B,G,D,F,R,T,B,G,S,Q]
MylistB=[B,G,D,F,R,T]
ListFound=0
Pos1=0
Pos2=1
位置3=2
位置4=3
位置5=4
位置6=5
Pos1A=0
Pos2A=1
Pos3A=2
Pos4A=3
Pos5A=4
Pos6A=5
当Pos6=len(ListA)时:
打印(“MylistB已找到”,ListFound,“MylistA内的时间”)
Pos1+=1
Pos2+=1
Pos3+=1
Pos4+=1
Pos5+=1
Pos6+=1

这就像预期的一样,但是占用了很多行,我正在寻找一种有效的方法来实现相同的结果。谢谢你的帮助。

以下是我的做法:

def compare(lst_a, lst_b):
    try:
        temp = [lst_a.index(x) for x in lst_b]
    except ValueError:
        res = False
    else:
        res = temp == sorted(temp)
    return res
一些测试运行:

ListA = ['A', 'B', 'G', 'S', 'X'] 
ListB = ['A', 'G', 'B']
ListC = ['A', 'B', 'G']
ListD = ['A', 'B', 'FOO']

print(compare(ListA, ListB))  #-> False
print(compare(ListA, ListC))  #-> True
print(compare(ListA, ListD))  #-> False ('FOO' does not exist at all in ListA)


这是通过从
ListA
获取
ListB
中所有条目的索引,并将它们存储在新列表
temp
中来实现的。如果
temp
已排序(
temp==sorted(temp)
),则您的规则已得到遵守,否则不会得到遵守。

在比较之前将列表转换为字符串将允许您在3行中执行此操作:

代码

   ListA = [10,2,3,4,5,6,7]
   ListB = [10,2]

   str1 = ' '.join(str(e) for e in ListA)
   str2 = ' '.join(str(e) for e in ListB)

   print(str2 in str1)
输出

>>>true

您可以创建如下内容:

ListA = ["A", "Q", "V", "B", "G", "D", "F", "R", "T", "B", "G", "S", "Q"]
ListB = ["B", "G", "D", "F", "R", "T"]

for x in range(0, len(ListA)):
    if ListA[x:len(ListB)+x] == ListB:
        print("Full Match", ListA[x:len(ListB)+x])
        print("Positions", "{}:{}".format(x, len(ListB)+x))
        break

# Full Match ['B', 'G', 'D', 'F', 'R', 'T']
# Positions 3:9 # last value (9) is exclusive


您可以尝试检查ListA中ListB每个元素的索引,然后检查它们的顺序是否正确:

ListA = ["A","Q","V","B","G","D","F","R","T","B","G","S","Q"]
ListB = ["B","G","D","F","R","T"]

indices=[]
for k in ListB:
  indices.append(ListA.index(k))

if sorted(indices) == indices:
   print "ListB is in ListA in the correct order"
else:
   print "ListB is not in ListA in the correct order"

“如预期的那样工作”:
ListA
ListB
不是有效的python列表,您是如何运行代码的?您的列表元素始终是字符串吗?是否希望ListB=['a','B','X']为true或false?顺序是保留的,但在这种情况下,ListB是分布在ListA中的。是的,它们总是作为字符串保留@kerweiI希望这是错误的,因为我需要它们以连续的顺序@Brevnoy你可以只做
打印(str1中的str2)
如果
ListA=[10,2]
ListB=[1,0],这也会产生误报
例如,您需要比较两个列表的长度,并在较长的连接字符串中断言较短的连接字符串
,否则它将失败。这种方法应该适用于单个字母字符串元素,但如果它们是数字字符串,那么它将是一个糟糕的选择,即
['1','12']
['11','2']
索引器
ValueError
?@Poolka捕捉得很好。更新。谢谢如果
ListA=['A','B','X','S','G']
呢?我想结果是一样的。可以吗?我试过了,效果很好,谢谢。我怎样才能突出显示在哪个位置找到了比赛?欢迎@和服1。当然,我会更新答案以反映您的评论。谢谢你是一个好公民,接受了正确的答案!ListA=[“C”、“A”、“B”、“C”]ListB=ListA[1:]将以不正确的顺序显示这将不保留顺序
ListA = ["A","Q","V","B","G","D","F","R","T","B","G","S","Q"]
ListB = ["B","G","D","F","R","T"]

indices=[]
for k in ListB:
  indices.append(ListA.index(k))

if sorted(indices) == indices:
   print "ListB is in ListA in the correct order"
else:
   print "ListB is not in ListA in the correct order"