如何比较 python 元素中的两个二维列表?

How to compare two 2d lists in python element wise?

我有两个二维列表: 两者大小相同,大小未知(不同列表集不同)

例如:

A = [['ID', 'Name', 'Profession'], [1, 'Tom', 'Teacher'], [2, 'Dick', 'Actor'], [3, 'Harry', 'Lawyer']]

B = [['ID', 'Name', 'Profession'], [1, 'Tom', 'Police'], [2, 'Dick', 'Actor'], [3, 'Harry', 'Lawyer']]

我想比较所有元素的文件元素(例如:a[0][1] == b[0][1])并打印元素索引的差异。

我想要这样的输出:

a[1][2] = Teacher <> b[1][2] = Police

如果我可以使用主键 (ID) 比较列表,以防列表不按顺序输出如下:

Profession of ID = 1 does not match, i.e Teacher <> Police

注意:文件可能很大(100*10000的矩阵)

谢谢。

试试这个:

A = [['ID', 'Name', 'Profession'], [1, 'Tom', 'Teacher'], [2, 'Dick', 'Actor'], [3, 'Harry', 'Lawyer']]

B = [['ID', 'Name', 'Profession'], [1, 'Tom', 'Police'], [2, 'Dick', 'Actor'], [3, 'Harry', 'Lawyer']]

# If A and B have equal length
for k in range(len(A)):
    i = A[k]
    j = B[k]

    # If the nested lists of both A and B has same length
    l = len(i)-1
    while(l>=0):
        if not i[l] is j[l]:
            print(f"a[{k}][{l}] = {i[l]} <> b[{k}][{l}] = {j[l]}")
        l -= 1

下面的代码应该可以完成工作:

for i in range(len(A)):
        B[i]
        A[i]
        if A[i] == B[i]: continue
        print(f'Differences in row{i}:', end='\n')
        for j in range(len(A[i])):
            if A[i][j] != B[i][j]:
                print(f'    in col {j}: A = {A[i][j]}, B = {B[i][j]}', end='\n')

对于给定的 A、B,它将打印:

Differences in row1:
    in col 2: A = Teacher, B = Police

应该适用于您决定输入的任意数量的变量。 请注意 f strings 仅在 python 3.6 中引入,因此如果您有错误可以更改为 string.format

你可以这样做:

A = [['ID', 'Name', 'Profession'], [1, 'Tom', 'Teacher'], [2, 'Dick', 'Actor'], [3, 'Harry', 'Lawyer']]

B = [['ID', 'Name', 'Profession'], [1, 'Tom', 'Police'], [2, 'Dick', 'Actor'], [3, 'Harry', 'Lawyer']]

A = {a[0]: {'Name': a[1], 'Profession': a[2]} for a in A[1:]}
B = {b[0]: {'Name': b[1], 'Profession': b[2]} for b in B[1:]}

for a_id, a_content in A.items():
    a_profession = a_content['Profession']
    b_profession = B[a_id]['Profession']
    equal_profession = a_profession == b_profession
    match = 'matches' if equal_profession else 'does not match'
    diff_profession = f", i.e {a_profession} <> {b_profession}" if not equal_profession else ''
    print(f"Profession of ID = {a_id} {match}{diff_profession}")

输出:

>>> Profession of ID = 1 does not match, i.e Teacher <> Police
>>> Profession of ID = 2 matches
>>> Profession of ID = 3 matches

要使用主键比较列表元素,让我们使用字典通过键对它们进行索引:

A_dict = {a[0]: a[1:] for a in A}
B_dict = {b[0]: b[1:] for b in B}

然后,迭代键和列并打印差异:

column_names = A[0][1:]
for id in A_dict.keys():
    for column in range(len(column_names)):
        if A_dict[id][column] != B_dict[id][column]:
            print(f"{column_names[column]} of ID = {id} does not match, "
                  f"i.e {A_dict[id][column]} <> {B_dict[id][column]}")

它给出了你想要的输出:

Profession of ID = 1 does not match, i.e Teacher <> Police

编辑:为了回答您的评论,如果第一个冒号中不需要您的 ID,则稍微复杂一些:

# get the number of the 'ID' column
column_names = A[0]
column_id = column_names.index('ID')

# get the column names without 'ID'
values_name = column_names[0:column_id] + column_names [column_id+1:]

# create a dictionary with keys in column `column_id`
# and values the list of the other column values
A_dict = {a[column_id]: a[0:column_id] + a[column_id+1:] for a in A}
B_dict = {b[column_id]: b[0:column_id] + b[column_id+1:] for b in B}

# iterate on the keys and on the other columns and print the differences
for id in A_dict.keys():
    for column in range(len(column_names) - 1):
        if A_dict[id][column] != B_dict[id][column]:
            print(f"{values_name[column]} of ID = {id} does not match, "
                  f"i.e {A_dict[id][column]} <> {B_dict[id][column]}")