试试这个
dict1={'apple':[1,2,3,4],'banana':[2,4,7,8],'orange':[5,6,2,4,7]}
dict2={'1':[2,4,7,8,9,3],'2':[5,6,2,4,7,1],'4':[1,2,3,4]}
for key1, val1 in dict1.items():
for key2, val2 in dict2.items():
print("Matching {}:{} and {}:{}".format(key1, val1, key2, val2))
diff = list(set(val1).symmetric_difference(val2))
if len(diff) != 0:
print("Differences found {}:{}\n".format(key2, diff))
else:
print("The two lists matched\n")
输出
Matching orange:[5, 6, 2, 4, 7] and 1:[2, 4, 7, 8, 9, 3]
Differences found 1:[3, 5, 6, 8, 9]
Matching orange:[5, 6, 2, 4, 7] and 2:[5, 6, 2, 4, 7, 1]
Differences found 2:[1]
Matching orange:[5, 6, 2, 4, 7] and 4:[1, 2, 3, 4]
Differences found 4:[1, 3, 5, 6, 7]
Matching apple:[1, 2, 3, 4] and 1:[2, 4, 7, 8, 9, 3]
Differences found 1:[1, 7, 8, 9]
Matching apple:[1, 2, 3, 4] and 2:[5, 6, 2, 4, 7, 1]
Differences found 2:[3, 5, 6, 7]
Matching apple:[1, 2, 3, 4] and 4:[1, 2, 3, 4]
The two lists matched
Matching banana:[2, 4, 7, 8] and 1:[2, 4, 7, 8, 9, 3]
Differences found 1:[3, 9]
Matching banana:[2, 4, 7, 8] and 2:[5, 6, 2, 4, 7, 1]
Differences found 2:[1, 5, 6, 8]
Matching banana:[2, 4, 7, 8] and 4:[1, 2, 3, 4]
Differences found 4:[1, 3, 7, 8]
我已更新列表以生成匹配列表的案例,它与此案例 Matching apple:[1, 2, 3, 4] and 4:[1, 2, 3, 4] 匹配。