如何在python中比较三层字典,并只打印不匹配的值



我有两个Dict作为

RT = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Economy', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': 'CAD 999', 'Cancellation Type': 'GUAR'}}
AA = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Main Cabin', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': '(CAD 850 + fees)'}}

我正在尝试比较这个嵌套的Dict和打印值,它们不相同。

期望输出为:

Airline Class : 'Economy' 'Main Cabin'
Hotel Price : 'CAD 999' '(CAD 850 + fees)'
Cancellation Type: 'GUAR' 'NA'
RT = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Economy', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': 'CAD 999', 'Cancellation Type': 'GUAR'}}
AA = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Main Cabin', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': '(CAD 850 + fees)'}}
ans = {}
for k,v in RT.items():
cur = []
for k_, v_ in v.items():
if k_ in AA[k]:
if (AA[k][k_]) != v_:
cur.append(AA[k][k_])
cur.append(v_)
ans[k_] = cur
cur = []
else:
cur.append(v_)
cur.append("NA")
ans[k_] = cur
cur =[]

for k,v in ans.items():
print(k, v)

相关内容

  • 没有找到相关文章

最新更新