如何一次添加多个嵌套词典?

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

我有一个方法,需要能够一次添加多个嵌套字典。

例如:

mydict = {}
mydict['subdict']['subdict2'] = {'this': 'is what i want'}

我该怎么做?

python python-3.x dictionary nested
2个回答
0
投票
from collections import defaultdict
mydict = defaultdict(dict)
mydict['subdict']['subdict2'] = {'this': 'is what i want'}

0
投票

我会在字典内创建一个空字典,以便嵌套字典。

这里;

my_dict = {}
my_dict["nested_item_1"] = {}
my_dict["nested_item_1"] = your_variable_here
© www.soinside.com 2019 - 2024. All rights reserved.