简体   繁体   中英

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? Identical, as in 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. 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:

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.

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 . 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, == . 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 operator

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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