我有两个 2d 列表: 两者大小相同,大小未知(不同的列表集不同(
例如:
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']]
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
以下代码应完成工作:
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']]
# 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
要使用主键比较列表元素,让我们使用字典按键索引它们:
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]}")