【问题标题】:How to check if two lists of tuples are identical如何检查两个元组列表是否相同
【发布时间】:2017-03-25 20:41:58
【问题描述】:

我需要检查元组列表是否按元组的第一个属性排序。最初,我想对照它的排序自我检查这个列表。比如……

list1 = [(1, 2), (4, 6), (3, 10)]
sortedlist1 = sorted(list1, reverse=True)

然后如何检查 list1 是否与 sortedlist1 相同?相同,如list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1]

列表的长度可能为 5 或 100,因此执行list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1] 不是一个选项,因为我不确定列表有多长。 谢谢

【问题讨论】:

    标签: python arrays list sorting tuples


    【解决方案1】:

    我相信你可以只做list1 == sortedlist1,而不必单独查看每个元素。

    【讨论】:

    • 这是正确的,因为相似类型的序列支持迭代字典比较。请参阅表格here 后面的注释。因此,相等比较会逐步遍历序列,直到遇到不同的对或序列的末尾(在前一种情况下返回 False,在后一种情况下返回 True)。
    【解决方案2】:

    如果您想检查列表是否已排序,请想到一个非常简单的解决方案:

    last_elem, is_sorted = None, True
    for elem in mylist:
        if last_elem is not None:
            if elem[0] < last_elem[0]:
                is_sorted = False
                break
        last_elem = elem
    

    这还有一个额外的好处,就是只浏览您的列表一次。如果你对它进行排序然后比较它,你至少会遍历列表不止一次。

    如果你还想这样做,这里有另一种方法:

    list1 = [(1, 2), (4, 6), (3, 10)]
    sortedlist1 = sorted(list1, reverse=True)
    all_equal = all(i[0] == j[0] for i, j in zip(list1, sortedlist1))
    

    【讨论】:

      【解决方案3】:

      @joce 已经提供了an excellent answer(我建议接受那个更简洁并直接回答您的问题),但我想解决您原始帖子的这一部分:

      列表的长度可能为 5 或 100,因此执行 list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1] 不是一个选项,因为我不确定列表有多长。

      如果您想比较两个列表的每个元素,您不需要知道列表的确切长度。编程就是懒惰,所以你可以打赌没有一个优秀的程序员会手工写出这么多的比较!

      相反,我们可以使用 index 遍历这两个列表。这将允许我们同时对两个列表的每个元素执行操作。这是一个例子:

      def compare_lists(list1, list2):
          # Let's initialize our index to the first element
          # in any list: element #0.
          i = 0
      
          # And now we walk through the lists. We have to be
          # careful that we do not walk outside the lists,
          # though...
          while i < len(list1) and i < len(list2):
              if list1[i] != list2[i]:
                  # If any two elements are not equal, say so.
                  return False
      
          # We made it all the way through at least one list.
          # However, they may have been different lengths. We
          # should check that the index is at the end of both
          # lists.
          if i != (len(list1) - 1) or i != (len(list2) - 2):
              # The index is not at the end of one of the lists.
              return False
      
          # At this point we know two things:
          #  1. Each element we compared was equal.
          #  2. The index is at the end of both lists.
          # Therefore, we compared every element of both lists
          # and they were equal. So we can safely say the lists
          # are in fact equal.
          return True
      

      也就是说,通过质量运算符== 检查 Python 是否具有内置此功能是很常见的事情。所以写起来要容易得多:

      list1 == list2
      

      【讨论】:

        【解决方案4】:

        python 3.x,你可以检查两个元组列表 ab 使用 eq 运算符相等

        import operator
        
        a = [(1,2),(3,4)]
        b = [(3,4),(1,2)]
        # convert both lists to sets before calling the eq function
        print(operator.eq(set(a),set(b))) #True
        

        【讨论】:

          【解决方案5】:

          使用这个:

          sorted(list1) == sorted(list2)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多