python中的字典比较

时间:2019-02-04 05:54:14

标签: python python-3.x dictionary

两个字典分别为:

dict1 = {'providers': 'aetna, united_health, cardinal'}
dict2 = {'aetna': ['13e2345', '13e2346'], 'bcbs': ['901E12'], 'united_health': []}

如果与dict1中的数据匹配,我需要比较两个字典并打印dict2中的数据。

预期输出:

{'aetna': ['13e2345', '13e2346'], 'united_health': []}

2 个答案:

答案 0 :(得分:3)

您可以使用简单的字典理解:

{'aetna': ['13e2345', '13e2346'], 'united_health': []}

输出:

{{1}}

答案 1 :(得分:0)

dict1 = {'providers': 'aetna, united_health'}
dict2 = {'aetna': ['13e2345', '13e2346'], 'bcbs': ['901E12'], 'united_health': []}

result = {key: dict2.get(key, []) for key in dict1.get('providers').split(', ')}

输出:

{'aetna': ['13e2345', '13e2346'], 'united_health': []}