【发布时间】:2020-08-07 19:11:25
【问题描述】:
我有来自服务器的 2 个不同的响应,具有相同的键,我将它们转换为字典:
dict1 = {'111': ['one', 1],
'bla': ['blaa', blaa],
'222': ['two', 2],
'bla1: ['bla2', bla3],
'333': ['three', 3],
}
dict2 = {'111': ['no matter what is here1'],
'AAA': ['no matter what is here2'],
'222': ['no matter what is here3'],
'BBB': ['no matter what is here4'],
'333': ['no matter what is here5'],
}
dict2 中存储什么值对我来说并不重要,我需要让 dict2 在 dict1 中找到相同的键,并将 dict1 中的完整项放入新的 dict3 字典中。
输出应该是这样的:
dict3 = {'111': ['one', 1],
'222': ['two', 2],
'333': ['three', 3],
粗略地说,dict2的key是指针,dict1中的item要放入dict3
解决方案不是 def () 对我来说很重要
【问题讨论】:
-
{key:dict1[key] for key in set(dict1.keys())&set(dict2.keys())}适合你吗? -
@Arjun Muraleedharan 是的.. 谢谢
标签: python python-3.x dictionary