【问题标题】:python - Comparing two lists to see if one occurs in another consecutivelypython - 比较两个列表以查看一个列表是否连续出现在另一个列表中
【发布时间】:2017-12-27 14:00:52
【问题描述】:

我一直在尝试创建一个函数,它可以采用两个任意大小的列表(例如,列表 A 和列表 B)并查看列表 B 是否出现在列表 A 中,但顺序相同。如果以上为真,则返回 True,否则返回 False

例如

A:[9,0,**1,2,3,4,5,6,**7,8] and B:[1,2,3,4,5,6] is successful

A:[1,2,0,3,4,0,5,6,0] and B:[1,2,3,4,5,6] is unsuccessful.

A:[1,2,3,4,5,6] and B [6,5,3,2,1,4] fails because despite having the same 
 numbers, they aren't in the same order

到目前为止,我已经尝试过使用嵌套循环来执行此操作,但对于去哪里有点困惑

【问题讨论】:

标签: python python-3.x list compare nested-loops


【解决方案1】:

试试这个:

L1 = [9,0,1,2,3,4,5,6,7,8]
L2 = [1,2,3,4,5,6]
c = 0
w = 0
for a in range(len(L2)):
   for b in range(w+1, len(L1)):
      if L2[a] == L1[b]:
        c = c+1
        w = b
        break
      else:
        c = 0
    if c == len(L2):
       print('yes')
       break

在这里,您检查 l2 的元素是否在 l1 中,如果是,则中断第一个循环,记住您离开的位置,并且 l2 的下一个元素与 l1 的下一个元素相同,依此类推。

最后一部分是检查这是否与 l2 的长度一样多。如果是这样,那么您就知道该陈述是正确的!

【讨论】:

  • 谢谢。我在标题旁边尝试了一些东西,但无法理解索引应该是什么。
  • 没问题,如果这行得通,那么请接受答案:)
【解决方案2】:

如果您的数组不是很大,并且您可以找到一种方法将数组中的每个元素映射到可以使用的字符串:

list1 = [9,0,1,2,3,4,5,6,7,8]
list2 = [1,2,3,4,5,6]

if ''.join(str(e) for e in list2) in ''.join(str(e) for e in list1):
    print 'true'

它只是从列表中创建两个字符串,然后使用 'in' 来查找任何匹配项

【讨论】:

    【解决方案3】:

    使用任何功能

    any(A[i:i+len(B)] == B  for i in range(len(A) - len(B) + 1))
    

    demo

    【讨论】:

      【解决方案4】:

      我将整个列表转换为字符串,然后找到该字符串的子字符串

      列表转换成字符串后变成了

      str(a)='[9,0,1,2,3,4,5,6,7,8]'
      

      当我们剥离字符串时,它变成了

      str(a).strip('[]')='9,0,1,2,3,4,5,6,7,8'
      

      现在问题刚刚转化为

      检查字符串中是否有子字符串 所以我们可以使用 in 运算符来检查子字符串

      解决方案

      a=[9,0,1,2,3,4,5,6,7,8]
      b=[1,2,3,4,5,6]
      print(str(b).strip('[]') in str(a).strip(']['))
      

      testcase1

      testcase2

      【讨论】:

        【解决方案5】:

        试试这个:

        L1 = [9,2,1,2,0,4,5,6,7,8]
        L2 = [1,2,3,4,5,6]
        def sameorder(L1,L2):
            for i in range(len(L1)-len(L2)+1):
                if L1[i:len(L2)+i]==L2:
                    return True
            return False
        

        【讨论】:

          【解决方案6】:

          您可以创建可分析的a 的子列表:

          def is_consecutive(a, b):
             return any(all(c == d for c, d in zip(b, i)) for i in [a[e:e+len(b)] for e in range(len(a)-len(b))])
          
          cases = [[[9, 0, 1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6]], [[1, 2, 0, 3, 4, 0, 5, 6, 0], [1, 2, 3, 4, 5, 6]], [[1, 2, 3, 4, 5, 6], [6, 5, 3, 2, 1, 4]]]
          final_cases = {"case_{}".format(i):is_consecutive(*a) for i, a in enumerate(cases, start=1)}    
          

          输出:

          {'case_3': False, 'case_2': False, 'case_1': True}
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-02-22
            • 1970-01-01
            • 1970-01-01
            • 2022-01-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-04
            相关资源
            最近更新 更多