简体   繁体   English

将字典转换为嵌套字典列表

[英]Converting dictionary into list of nested dictionary

I have a list of the dictionary and other list List_1=[D1,D2,D3] .我有一个字典列表和其他列表List_1=[D1,D2,D3] My list of dictionary test_dict is like:`我的字典 test_dict 列表如下:`

[{"D1": 1.0,"D2": 0},{"D4":1.2,"D2":1.4}]

What I want in my final_dict is:我在 final_dict 中想要的是:

[{"List_1":D1,"value":1.0,"List_1":D2,"value":0},{"Default":D4,"value":1.2,"List_1":D2,"value":1.4}]

In test_dict at index zero i have a dictionary having D1 and D2 as key and 1.0 and 0 are there values.But in my final_dict.在索引为零的 test_dict 中,我有一个字典,其中 D1 和 D2 作为键,1.0 和 0 有值。但在我的 final_dict 中。 I want to change my key value pair.我想更改我的键值对。 My final goal is to insert it into mongodb link我的最终目标是将其插入 mongodb 链接

Key should not be the same in a dictionary else the values will get overwritten.字典中的键不应相同,否则值将被覆盖。 For example, the output of your expectation will be:-例如,您期望的输出将是:-

{'List_1': 'D2', 'value': 0}

So, you need to assign values with different keys.因此,您需要使用不同的键分配值。

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

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