简体   繁体   English

如何检查两个元组列表是否相同

[英]How to check if two lists of tuples are identical

I need to check to see if a list of tuples is sorted, by the first attribute of the tuple.我需要检查元组列表是否按元组的第一个属性排序。 Initially, I thaught to check this list against its sorted self.最初,我想根据它的排序自我检查这个列表。 such as...比如……

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

How can I then check to see if list1 is identical to sortedlist1?然后我如何检查 list1 是否与 sortedlist1 相同? Identical, as in list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1] .相同,如list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1]

The list may have a length of 5 or possibly 100, so carrying out list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1] would not be an option because I am not sure how long the list is.该列表的长度可能为 5 或 100,因此执行list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1]将不是一个选项,因为我不确定名单是。 Thanks谢谢

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

@joce already provided an excellent answer (and I would suggest accepting that one as it is more concise and directly answers your question), but I wanted to address this portion of your original post: @joce 已经提供了一个很好的答案(我建议接受它,因为它更简洁并且直接回答了您的问题),但我想解决您原始帖子的这一部分:

The list may have a length of 5 or possibly 100, so carrying out list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1] would not be an option because I am not sure how long the list is.该列表的长度可能为 5 或 100,因此执行list1[0] == sortedlist1[0], and list1[1] == sortedlist1[1]将不是一个选项,因为我不确定名单是。

If you want to compare every element of two lists, you do not need to know exactly how long the lists are.如果要比较两个列表的每个元素,则无需确切知道列表的长度。 Programming is all about being lazy, so you can bet no good programmer would write out that many comparisons by hand!编程就是偷懒,所以你可以打赌,没有一个好的程序员会手工写出这么多的比较!

Instead, we can iterate through both lists with an index .相反,我们可以使用index遍历两个列表。 This will allow us to perform operations on each element of the two lists simultaneously.这将允许我们同时对两个列表的每个元素执行操作。 Here's an example:下面是一个例子:

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

That said, this is such a common thing to check for that Python has this functionality built in through the quality operator, == .也就是说,检查 Python 是否通过质量运算符==内置了此功能是一件很常见的事情。 So it's much easier to simply write:因此,简单地编写要容易得多:

list1 == list2

If you want to check if a list is sorted or not a very simple solution comes to mind:如果您想检查列表是否已排序,我会想到一个非常简单的解决方案:

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

This has the added advantage of only going over your list once.这有一个额外的好处,就是只检查你的列表一次。 If you sort it and then compare it, you're going over the list at least greater than once.如果您对它进行排序然后比较它,那么您至少要查看列表不止一次。

If you still want to do it that way, here's another method:如果您仍然想这样做,这是另一种方法:

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))

In python 3.x , you can check if two lists of tuples a and b are equal using the eq operatorpython 3.x ,您可以使用eq运算符检查两个元组ab列表是否相等

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

使用这个:

sorted(list1) == sorted(list2)

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

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