如何按顺序检查一个列表是否是另一个列表的子序列

我有一个清单input:


[1, 2, 4, 3, 5, 7, 5, 3, 8, 3, 8, 5, 8, 5, 9, 5, 7, 5, 7, 4, 9, 7, 5, 7, 4, 7, 4, 7, 8, 9, 7, 5, 7, 5, 4, 9, 3, 4, 8, 4, 8, 5, 3, 5, 4, 7, 3, 7, 3, 1, 2, 7, 1, 7, 2, 1]

我需要检查 a 的元素是否lookup_list [1,2,3,4,5,7,8,9,5,4,3,2,1]以相同的顺序以分散的方式出现在上面的列表中。


这将说明我想说的话:


[1, 2, 4, 3, 5, 7, 5, 3, 8, 3, 8, 5, 8, 5, 9, 5, 7, 5, 7, 4, 9, 7, 5, 7, 4, 7, 4, 7, 8, 9, 7, 5, 7, 5, 4, 9, 3, 4, 8, 4, 8, 5, 3, 5, 4, 7, 3, 7, 3, 1, 2, 7, 1, 7, 2, 1]


粗体数字是列表中顺序相同的lookup_list当前数字,但中间还有其他不相关的项目。input


有什么办法可以检查这个吗?


这是我试过的方法:


count = 0

a = 0

indices = []

for item in list:

    idx = -1

    if count < len(input_list):

        idx = list.index(input_list[count])

        if idx != -1:

            a = a +len(list[:idx])

            list = list[idx:]

            indices.append(a + idx)

    count = count +1

print(indices)

但它给了我以下结果:


[0, 2, 5, 35, 25, 24, 33, 30, 33, 37, 38, 64, 54]

lookup_list问题是,此方法中未维护的顺序。


慕慕森
浏览 124回答 3
3回答

Smart猫小萌

您可以在输入列表上使用迭代器。调用next以获取每个值。如果您用尽迭代器而没有找到所有匹配项,您将得到 aStopIteration然后返回False。def check(input_, lookup_list):&nbsp; &nbsp; it = iter(input_)&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; for i in lookup_list:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # read values from the input list until we find one which&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # matches the current value from the lookup list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while next(it) != i:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; &nbsp; &nbsp; # we found matches for everything on the lookup list&nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; except StopIteration:&nbsp; &nbsp; &nbsp; &nbsp; # we tried to go past the end of the input list&nbsp; &nbsp; &nbsp; &nbsp; return False

拉风的咖菲猫

def check_exists(l, lookup_list):check = Truefor i in lookup_list:&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; index = l.index(i)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; l = l[index+1:]&nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; check = False&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp;return checkcheck_exists()函数将接受完整列表和查找列表作为参数,如果序列存在则返回 True,否则返回 false。这是完整的程序-def check_exists(l, lookup_list):&nbsp; &nbsp; check = True&nbsp; &nbsp; for i in lookup_list:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = l.index(i)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l = l[index+1:]&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; check = False&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp;&nbsp; &nbsp; return checkl = [1, 2, 4, 3, 5, 7, 5, 3, 8, 3, 8, 5, 8, 5, 9, 5, 7, 5, 7, 4, 9, 7, 5, 7,&nbsp;4, 7, 4, 7, 8, 9, 7, 5, 7, 5, 4, 9, 3, 4, 8, 4, 8, 5, 3, 5, 4, 7, 3, 7, 3, 1,&nbsp;2, 7, 1, 7, 2, 1]lookup_list = [2,3,4,5,7,8,9,5,4,3,2,1]&nbsp;print(check_exists(l,lookup_list))

繁星淼淼

最简单的方法是将第一个列表(不要称它为!)转换为字符串input(尖括号将多位数字“放在一起”):input_str&nbsp;=&nbsp;"".join("<"+str(i)+">"&nbsp;for&nbsp;i&nbsp;in&nbsp;input_list)将第二个列表转换为正则表达式,允许在必需项之间添加可选的附加项:lookup_str&nbsp;=&nbsp;".*"&nbsp;+&nbsp;".+".join("<"+str(i)+">"&nbsp;for&nbsp;i&nbsp;in&nbsp;lookup_list)&nbsp;+&nbsp;".+"现在,检查输入字符串是否与正则表达式匹配:if&nbsp;(re.search(lookup_str,&nbsp;input_str)):&nbsp;#&nbsp;there&nbsp;is&nbsp;a&nbsp;match
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python