简体   繁体   English

比较2个不同的列表

[英]Comparing 2 different list of lists

for example there are the following 2 list of lists 例如,有以下2个列表列表

A = [[0, 1, 2, 1, 9], [1, 0, 0, 6, 0], [2, 0, 0, 15, 2], [1, 6, 15, 0, 7], [9, 0, 2, 7, 0]]

B = [[0, 19, 1, 0, 12], [19, 0, 2, 0, 0], [1, 2, 0, 0, 2], [0, 0, 0, 0, 3], [12, 0, 2, 3, 0]]

so what i want to do is, i want to compare each value in the 2 different list of lists like. 所以我想做的是,我想比较2个不同的列表中的每个值。 i want to compare A[0][0] with B[0][0] but not A[0][1] with B[0][0]. 我想将A [0] [0]与B [0] [0]进行比较,而不是将A [0] [1]与B [0] [0]进行比较。 so basically i want to compare like the first value in the first list in A with the respectively value in B and so on. 所以基本上我想像A中的第一个列表中的第一个值与B中的相应值进行比较,依此类推。 how can i do this? 我怎样才能做到这一点? thank you very much :) EDIT: sorry. 非常感谢:)编辑:对不起。 my lengths of the lists were different. 我的名单长短不一。 this is the updated version . 这是更新的版本。

If you need to know if they are identical just run this: 如果您需要知道它们是否相同,请运行以下命令:

list1 = [['first',1], ['second',2], ['third',3]]
list2 = [['first',1], ['second',2], ['third',3]]
print(sorted(list1) == sorted(list2))

Result: true 结果: true

You could use a double list comprehension to always select the largest value and create a new, flat list: 您可以使用双重列表理解来始终选择最大值并创建一个新的平面列表:

>>> A = [[0, 1, 2, 1, 9], [1, 0, 0, 6, 0], [2, 0, 0, 15, 2], [1, 6, 15, 0, 7], [9, 0, 2, 7, 0]]
>>> B = [[0, 19, 1, 0, 12], [19, 0, 2, 0, 0], [1, 2, 0, 0, 2], [0, 0, 0, 0, 3], [12, 0, 2, 3, 0]]
>>> [max(a,b) for la,lb in zip(A,B) for a,b in zip(la,lb)]
[0, 19, 2, 1, 12, 19, 0, 2, 6, 0, 2, 2, 0, 15, 2, 1, 6, 15, 0, 7, 12, 0, 2, 7, 0]

If you want to keep the 2-D structure, you can use a nested list comprehension: 如果要保留二维结构,则可以使用嵌套列表理解:

>>> [[max(a,b) for a,b in zip(la,lb)] for la,lb in zip(A,B)]
[[0, 19, 2, 1, 12], [19, 0, 2, 6, 0], [2, 2, 0, 15, 2], [1, 6, 15, 0, 7], [12, 0, 2, 7, 0]]

To check if two lists are equal you simply do equality check like this: 要检查两个列表是否相等,您只需执行以下相等检查:

A == B

But if you are interested in knowing which elements of the sublists differ or alternatively, which positions or index "coordinates" for which the lists differ then you might want to do this: 但是,如果您想知道子列表的哪些元素不同,或者想知道列表不同的位置或索引“坐标”,那么您可能想要这样做:

A = [[0, 1, 2, 1, 9], [1, 0, 0, 6, 0], [2, 0, 0, 15, 2], [1, 6, 15, 0, 7], [9, 0, 2, 7, 0]]
B = [[0, 19, 1, 0, 12], [19, 0, 2, 0, 0], [1, 2, 0, 0, 2], [0, 0, 0, 0, 3], [12, 0, 2, 3, 0]]

differences = [
    (outer_idx, inner_idx)
    for outer_idx, (a, b) in enumerate(zip(A, B))
    for inner_idx, (a_element, b_element) in enumerate(zip(a, b))
    if a_element != b_element
]

print(differences)

# output
[(0, 1),
 (0, 2),
 (0, 3),
 (0, 4),
 (1, 0),
 (1, 2),
 (1, 3),
 (2, 0),
 (2, 1),
 (2, 3),
 (3, 0),
 (3, 1),
 (3, 2),
 (3, 4),
 (4, 0),
 (4, 3)]

All this means that for (0, 1) the lists are different. 所有这些意味着对于(0,1),列表是不同的。 ie: A[0][1] != B[0][1] and so on. 即: A[0][1] != B[0][1] ,依此类推。

#returns True if f is larger than s
def compare(f, s):
    print("comparing {} and {}".format(f,s))
    return f > s

def compare_lists(A,B):
    for sub_lists in zip(A,B):
        for first, second in zip(sub_lists[0], sub_lists[1]):
            compare(first, second)

You haven't specified what kind of compare you want. 您尚未指定所需的比较类型。 The other answers assume you want to check for equality. 其他答案假定您要检查是否相等。 This is a more general approach. 这是一种更通用的方法。 Put whatever compare logic you want into compare(f,s) 将您想要的任何比较逻辑放入compare(f,s)

Edit: Your sublists contain different amounts of elements. 编辑:您的子列表包含不同数量的元素。 This approach only compares according to the shortest sublist. 此方法仅根据最短的子列表进行比较。 So for [1,2] and [1] , only 1 and 1 is compared, since there is no matching entry for 2 from the first list. 因此,对于[1,2][1] ,仅比较11 ,因为从第一个列表中没有2匹配条目。

Edit2: You just edited your post. Edit2:您刚刚编辑了帖子。 I thought it was intentional that the lenghts were different... 我认为这是故意的,因为长度有所不同。

Assuming you want to know if they are identical: 假设您想知道它们是否相同:

def compare(a,b):
    if (len(a) != len(b) ) return false;
    for i in range(0, len(a)):
       if ( len(a[i]) != len(b[i]) ) return false;
       for j in range(0, len(a[i])):
           if (a[i][j]!=b[i][j]) return false;
    return true;

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

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