简体   繁体   English

将列表的字典转换为python中的字典字典

[英]convert a dictionary of list to a dictionary of dictionary in python

I have two dictionaries of slightly differing formats and I want to convert one to look like the other so that I can compare them easily. 我有两种格式略有不同的字典,我想将其中一种转换为另一种,以便我可以轻松比较它们。 Any pointers to help acheive this would be appreciated. 任何有助于实现此目的的指针将不胜感激。

Dict 1 --> {Key:[{key 1 : value: 1, key 2: value2}, {key 3: value 3, key 4: value 4}]}

Dict 2 --> {Key: { VALUE : [{key 1 : value: 1, key 2: value2}, {key 3: value 3, key 4: value 4}]}}

It seems the only difference between the two dictionaries is that the second one has an extra dict with key VALUE . 似乎两个字典之间的唯一区别是第二个字典的键值是VALUE You could change it to look like the first one by executing: 您可以通过执行以下命令将其更改为第一个:

dict2[Key] = dict2[Key][VALUE]

If you want to change the first dict to look like the second one, you could execute: 如果要将第一个字典更改为第二个字典,可以执行以下命令:

dict1[Key] = {VALUE: dict1[Key]}

尝试这个:

dict2[Key]=dict2[Key][VALUE]

Taking dict_1 's structure to be the one you want (might be easier to use): 使dict_1的结构成为您想要的结构(可能更易于使用):

d_3 = {}

d_3[key] = dict_2[key][VALUE] 

You can convert the first dict to the second dict by executing this: 您可以通过执行以下命令将第一个字典转换为第二个字典:

dict3[Value]=dict1[Key]
dict2[Key]=dict3

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM