简体   繁体   English

将字典中的值作为键插入另一个字典

[英]Inserting values from a dictionary as keys in another dictionary

I have two dictionaries, and i would like to create a third where the values from number 1 becomes the keys in number 2 - i have searched alot but i think since my second dictionary is in a nested form i have not seemed to find an example.我有两个字典,我想创建第三个字典,其中数字 1 的值成为数字 2 中的键 - 我搜索了很多,但我认为由于我的第二个字典是嵌套形式,我似乎没有找到示例. I am new to python, which maybe is why i have searched for the wrong things, and i hoped that posting here could help me solve it.我是 python 的新手,这可能是我搜索错误内容的原因,我希望在这里发帖可以帮助我解决它。

dict1 = {0: '123', 1: '456', 2:'789'}


dict 2 = 
{0: [{'id': 'abcd',
   'ocid': '134'},
  {'id': 'efgh',
   'ocid': '154'}],
1: {'id': 'rfgh',
   'ocid': '654'},
  {'id': 'kjil',
   'ocid': '874'}],
2: {'id': 'bgsj',
   'ocid': '840'},
  {'id': 'ebil',
   'ocid': '261'}]}

My desired output is: 

dict3 = 
{123: [{'id': 'abcd',
   'ocid': '134'},
  {'id': 'efgh',
   'ocid': '154'}],
456: {'id': 'rfgh',
   'ocid': '654'},
  {'id': 'kjil',
   'ocid': '874'}],
789: {'id': 'bgsj',
   'ocid': '840'},
  {'id': 'ebil',
   'ocid': '261'}]} ```



As long as the two dictionaries are the same length and in the expected order to make the matches, you can iterate through the pairs of values in both dictionaries as follows:只要两个字典的长度相同并且按照预期的顺序进行匹配,您就可以遍历两个字典中的值对,如下所示:

keys = dict1.values()  # Get values from dict1
values = dict2.values()  # Get values from dict2

dict3 = {}  # Init new dict

# Iterate over tuples (dict1_value, dict2_value)
for key, value in zip(keys, values): 
   dict3[key] = value  # Use dict1_value as key and dict2_value as value

EDIT编辑

Extending my original answer with @deceze suggestion for the case when key in both dictionaries can be used to perform the matches:当两个字典中的键可用于执行匹配时,使用@deceze 建议扩展我的原始答案:

dict3 = {dict1[k]: v for k, v in dict2.items()}

I hope this works for you!我希望这对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个字典的值替换字典键 - Replace dictionary keys from values of another dictionary 从一个字典的键和另一个字典的值构建一个新字典 - Build a new dictionary from the keys of one dictionary and the values of another dictionary 通过比较来自另一本字典的键,从一本字典返回值 - Return values from one dictionary by comparing keys from another dictionary 从另一个字典创建字典,其中键'=(上一个字典中的值)和值'=(上一个字典中的共享值) - Creating a dictionary from another dictionary where keys' = (values in previous dictionary) and value' = (shared values in previous dictionary) Python遍历字典以用另一个字典中的值替换键 - Python looping over dictionary to substitute keys with the values from another dictionary 创建一个值与另一个字典中的键相同的字典 - Creating a dictionary with values identical to keys from another dictionary 使用相同的键附加来自另一个字典的字典值 - Append dictionary values from another dictionary with the same keys 如果键匹配,如何从另一个字典附加到字典值 - how to append to a dictionary values from another dictionary if keys match 用另一本词典中的键替换一个词典中的值 - Replacing values in one dictionary with the keys in another dictionary 在另一个字典中的键之间分配字典的值 - Distributing values of a dictionary between keys in another dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM