简体   繁体   English

python列表和子列表

[英]python list and sublist

I have to check if list1 is contained in list2. 我必须检查list2中是否包含list1。 It also should check if it appears in that order in the list as well. 它还应检查它是否也按列表中的顺序出现。 If it's true, it should return true and false if it doesn't. 如果是真的,它应该返回true,如果不是,则返回false。

def check(lst1, lst2):
for x in lst1:
    for y in lst2:
        xx = lst1.sort()
        yy = lst2
        if xx != yy:
            return False
        else:
            return True

I'm confusing myself with the for loops and also, I don't know where to go from here to fix my code. 我对for循环感到困惑,而且我不知道从哪里开始修复我的代码。 Pointers please? 指针好吗?

example of what it should do: 它应该做什么的例子:

  check([4,0],[9,1,4,6,8,9])
  True
  check([1,2],[2,3,1])
  False

I thought the problem was begging to be solved recursively, so I did: 我认为问题是要求递归解决,所以我做了:

def check(needle, haystack):
  if not needle:
      return True
  try:
    offset = haystack.index(needle[0])
    return check(needle[1:], haystack[offset+1:])
  except:
    return False

Edit: 编辑:

Brief explanation: 简要说明:

First we check if the list we're looking for is empty (this is important once we start calling ourselves), since all lists have the empty list inside it, we return True. 首先,我们检查我们要查找的列表是否为空(这一点在我们开始调用自己时很重要),因为所有列表中都有空列表,我们返回True。 Then we try finding the first item of the list we're looking for in the list we're looking at. 然后我们尝试在我们正在查看的列表中找到我们正在寻找的列表的第一项。 If we find it, we then call the function again, but change the arguments a bit: we already looked at the first item of the 'needle' and saw it in the 'haystack'. 如果我们找到它,我们再次调用该函数,但稍微更改一下参数:我们已经查看了'needle'的第一项,并在'haystack'中看到了它。 So now we need to check if the remainder of 'needle' is in the remainder of 'haystack'. 所以现在我们需要检查'needle'的剩余部分是否在'haystack'的剩余部分中。 So we call the function again only with the remainder of both lists. 所以我们只用两个列表的其余部分再次调用该函数。 The remainder of needle is everything but the first element, and the remainder of haystack is all items after the one we found. 针的剩余部分是除了第一个元素之外的所有东西,而干草堆的剩余部分是我们发现的之后的所有项目。 If we get to a point where the first list is empty, it means we found it in the haystack. 如果我们到达第一个列表为空的点,则意味着我们在大海捞针中找到它。 If we get an exception, what we were looking for wasn't found, so we return False, which bubbles up the call stack and gets returned. 如果我们得到一个异常,我们找不到的东西,所以我们返回False,它会调用堆栈并返回。

You could start with something like: 您可以从以下内容开始:

set(lst1).issubset(lst2)

to see if lst1 is contained within lst2 ignoring order. 查看lst1是否包含在lst2中忽略顺序。 If it passes the test that one list is contained within the other, then you could do something like: 如果它通过了测试,其中一个列表包含在另一个列表中,那么您可以执行以下操作:

ii = lst2.index(lst1[0])
if lst2[ii:ii+len(lst1)] == lst1:
   return True
else:
   return False

Originally I stated that the first check was irrelevant given the second, although you'd have to properly handle the ValueError if the first element of lst1 is not in lst2. 最初我说第一次检查与第二次检查无关,尽管如果lst1的第一个元素不在lst2中,你必须正确处理ValueError

Edit: Just as a side note, I compared a version of my code vs yan's and mine is significantly faster under almost all use cases, especially if len(lst1) is larger (up to 200x speedup vs yan's implementation). 编辑:正如旁注,我比较了我的代码与yan的版本,并且在几乎所有用例中我的速度明显更快,特别是如果len(lst1)更大(与yan的实现相比,加速速度高达200倍)。 Give it a try with the timeit module. 尝试使用timeit模块。

def check(lst1,lst2):
    try:
        ii = lst2.index(lst1[0])
    except ValueError:
        return False

    if lst2[ii:ii+len(lst1)] == lst1:
        return True
    else:
        return False 

As an explanation of how it works ii = lst2.index(lst1[0]) finds the index in lst2 that matches the first element of lst1 . 作为其工作原理的解释, ii = lst2.index(lst1[0])lst2中找到与lst1的第一个元素匹配的lst1 If that item is missing from lst2 it catches the ValueError and returns False . 如果lst2缺少该项,则捕获ValueError并返回False If that element does exist, lst2[ii:ii+len(lst1)] == lst1 compares all of lst1 vs the sublist of lst2 starting from the matched element and taking the next len(lst) elements. 如果该元素确实存在,则lst2[ii:ii+len(lst1)] == lst1将所有lst1lst2的子列表进行比较,从匹配元素开始并获取下一个len(lst)元素。

>>> a=[9,1,4,6,8,9]
>>> by_twos=zip(a, a[1:])
>>> by_twos
[(9, 1), (1, 4), (4, 6), (6, 8), (8, 9)]
>>> (4,6) in by_twos
True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM