用另一个字典的值替换字典的键

问题描述 投票:0回答:2

我有两个这样的词典

dict_1 = {'adbc.txt':'Text','union.pdf':'pdf', 'file1.py':'Python'}
dict_2 = {'adbc.txt':['a.txt','b.txt','c.txt'], 'file1.py':['test.py', 'modified.py']}

我需要在Python中获得以下结果。有人可以帮忙吗

result = {'a.txt':'Text','b.txt':'Text','c.txt':'Text','test.py':'Python', 'modified.py':'Python', 'union.pdf':'pdf'}

'adbc.txt''file1.py'键是两个词典中的通用键,我必须得到上面的结果。

python python-2.7 dictionary
2个回答
0
投票

您可以使用字典理解,例如:


0
投票

由于dict_1中的键之一不在dict_2中,因此列表理解将非常麻烦。一个简单的循环:

© www.soinside.com 2019 - 2024. All rights reserved.