有没有一种有效的方法来搜索列表,而另一个列表保持列表的顺序呢?

时间:2019-04-03 09:09:49

标签: python list

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

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

MylistB = [A, G, B]

我希望此方法返回false,因为ListBListA的顺序不同。但是,如果是这样:

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

这可以按预期工作,但是占用很多行,我正在寻找一种有效的方法来获得相同的结果。感谢您的帮助。

5 个答案:

答案 0 :(得分:3)

您可以创建以下内容:

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

Demo

答案 1 :(得分:0)

这是我要怎么做:

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)

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

答案 2 :(得分:0)

import collections 

inputList1 = [1, 2, 4, 3, 5] 
inputList2 = [1, 2, 4, 3, 5] 
print ("The first list is : " + str(inputList1)) 
print ("The second list is : " + str(inputList2)) 

# Using Sorting
inputList1.sort() 
inputList2.sort() 
if inputList1 == inputList2: 
    print ("The lists are identical") 
else : 
    print ("The lists are not identical")

# using Collection Counter  
if collections.Counter(inputList1) == collections.Counter(inputList2): 
    print ("The lists are identical") 
else : 
    print ("The lists are not identical")

答案 3 :(得分:0)

您可以尝试检查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"

答案 4 :(得分:-1)

在比较之前将列表转换为字符串将使您可以在 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