简体   繁体   English

如何找到两个元组列表之间的差异

[英]How to find the difference between two lists of tuples

I have two lists, which are the output of my previous codes:我有两个列表,它们是我之前代码的输出:

List_1 = [((1.1, 2, 3), (1.1, 2, 3, 4), (3, 4, 5), 5, 6, 7)]
List_2 = [((1.1, 2, 3), (1.1, 2, 3, 4), (3, 4.4, 5), 5, 6, 7)]

I know these two lists only have one element, and I am trying to remove the parentheses and make them like:我知道这两个列表只有一个元素,我正在尝试删除括号并使它们像:

List_1 = [(1.1, 2, 3), (1.1, 2, 3, 4), (3, 4, 5), 5, 6, 7]
List_2 = [(1.1, 2, 3), (1.1, 2, 3, 4), (3, 4.4, 5), 5, 6, 7]

What I want is to compare each element from two lists and output the elements of the second list which differ from those of the first list, like this:我想要的是比较两个列表中的每个元素并输出与第一个列表不同的第二个列表的元素,如下所示:

Error = (3, 4.4, 5)

Does anyone know how to compare it?有谁知道怎么比较? Thank you in advance.先感谢您。

you can use basic for loop and if statement to check values:您可以使用基本的 for 循环和 if 语句来检查值:

List_1 = [((1.1, 2, 3), (1.1, 2, 3, 4), (3, 4, 5), 5, 6, 7)]
List_2 = [((1.1, 2, 3), (1.1, 2, 3, 4), (3, 4.4, 5), 5, 6, 7)]

List_1 = list(List_1[0]) # [(1.1, 2, 3), (1.1, 2, 3, 4), (3, 4, 5), 5, 6, 7]
List_2 = list(List_2[0]) # [(1.1, 2, 3), (1.1, 2, 3, 4), (3, 4.4, 5), 5, 6, 7]

for i in range(len(List_1)):
    if List_1[i] != List_2[i]:
        print ("Error: {}".format(List_2[i]))

output:输出:

Error: (3, 4.4, 5)

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

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