问题
I have two 2d lists: Both are of same size with size unknown (different for different set of lists)
For instance:
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']]
I want to compare the files element wise (e.g: a[0][1] == b[0][1]
) for all the elements and print the difference with element index.
I would like to have output something like this:
a[1][2] = Teacher <> b[1][2] = Police
It would be great if I could compare the lists using primary key (ID) in case the list is not in order with output as below:
Profession of ID = 1 does not match, i.e Teacher <> Police
Note: file may be very huge (matrix of 100*10000)
Thanks.
回答1:
You can do this:
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}")
Which ouputs:
>>> Profession of ID = 1 does not match, i.e Teacher <> Police
>>> Profession of ID = 2 matches
>>> Profession of ID = 3 matches
回答2:
The following code should do the work:
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')
For given A, B it will print:
Differences in row1:
in col 2: A = Teacher, B = Police
Should work for any amount of variables you decide to input.
Please note f strings
were only introduced in python 3.6 so if you have errors you can change to string.format
回答3:
Try this:
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
回答4:
To compare your list elements using their primary key, let's index them by key using a dictionnary:
A_dict = {a[0]: a[1:] for a in A}
B_dict = {b[0]: b[1:] for b in B}
Then, iterate on the keys and columns and print the differences:
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]}")
Which gives the output you wanted:
Profession of ID = 1 does not match, i.e Teacher <> Police
Edit: To answer your comment, if your ID is not necessary in the first colon, it is a little more complicated:
# 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]}")
来源:https://stackoverflow.com/questions/59633163/how-to-compare-two-2d-lists-in-python-element-wise