【问题标题】:Deciding if two lists are cyclic permutations of eachother判断两个列表是否是彼此的循环排列
【发布时间】:2016-11-28 17:40:29
【问题描述】:

我被分配编写一个函数,该函数接受两个列表,如果另一个列表是另一个列表的循环置换,则返回 True。 我编写了一个函数,它接受两个列表并在第一个和最后一个位置之间进行更改。之后,我编写了一个函数,该函数使用 for 循环调用第一个函数,如果每个 i 都为真,则在循环结束时返回 True。 我尝试运行代码并遇到了几条错误消息:

文件“C:/WinPython-64bit-3.5.2.2Qt5/settings/.spyder-py3/temp.py”,第 13 行,循环 if change_position(lst1, lst2):

文件“C:/WinPython-64bit-3.5.2.2Qt5/settings/.spyder-py3/temp.py”,第 5 行,在 change_position lst3[0] = lst4[len(lst4)]

这是我的代码:

def change_position(lst3, lst4):
    if len(lst3) != len(lst4):
        print(False)
    else:
        lst3[0] = lst4[len(lst4)]


def cyclic(lst1, lst2):
    if len(lst1) != len(lst2):
        print(False)
    else:
        for i in range(len(lst1)):
            if change_position(lst1, lst2):
                print(True)
            else:
                print(False)
cyclic([1, 2, 3, 4], [4, 1, 2, 3])

有谁知道我该如何解决这个问题,以便该功能正常工作? 提前感谢您的所有帮助。

【问题讨论】:

    标签: python


    【解决方案1】:

    无需重新排列您的列表,只需计算索引即可

    def is_cyc_perm (list1, list2):
        if len (list1) == len (list2):
            for shift in range (len (list1)):
                for i in range (len (list1)):
                    if list1 [i] != list2 [(i + shift) % len (list1)]:
                        break
                else:
                    return True
            else:
                return False
        else:
            return False
    
    
    print (is_cyc_perm ([8, 2, 5, 7], [8, 2, 5, 7]))
    print (is_cyc_perm ([7, 8, 2, 5], [8, 2, 5, 7]))
    print (is_cyc_perm ([2, 5, 7, 8], [8, 2, 5, 7]))
    print (is_cyc_perm ([8, 5, 2, 7], [8, 2, 5, 7]))
    print (is_cyc_perm ([7, 2, 5, 8], [8, 2, 5, 7]))
    print (is_cyc_perm ([8, 2, 5, 3], [8, 2, 5, 7]))
    

    【讨论】:

    • 我知道你的代码有效,但我很好奇为什么你的代码中有“else:”,而只有 2 个“if”语句?
    【解决方案2】:

    列表索引从0开始,你应该使用range(0, len(list)-1)。

    def change_position(lst3, lst4):
        if len(lst3) != len(lst4):
            print(False)
        else:
            lst3[0] = lst4[len(lst4)-1]
    
    
    def cyclic(lst1, lst2):
        if len(lst1) != len(lst2):
            print(False)
        else:
            for i in range(len(lst1)-1):
                if change_position(lst1, lst2):
                    print(True)
                else:
                    print(False)
    

    【讨论】:

      【解决方案3】:

      旋转双链表是一种使用指针的快速操作。如果有匹配,您可以简单地尝试所有旋转。

      集合模块还提供Counter,如果两个列表具有相同元素的相同计数,可用于快速测试。它只是明显的相同长度检查的更强大版本。

      import collections
      
      def is_cyc_perm(seq1, seq2):
          mset1 = collections.Counter(seq1)
          mset2 = collections.Counter(seq2)
          if mset1 != mset2:
              return False
      
          size = len(seq1)
          deq1 = collections.deque(seq1)
          deq2 = collections.deque(seq2)
          for _ in range(size):
              deq2.rotate()
              if deq1 == deq2:
                  return True
          return False
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-28
        • 1970-01-01
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多