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

发布于 2025-01-16 04:22:06 字数 187 浏览 2 评论 0原文

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

例如:

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

我该怎么做?

I have a method which requires being able to add multiple nested dictionaries at once.

For example:

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

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2025-01-23 04:22:06
from collections import defaultdict
mydict = defaultdict(dict)
mydict['subdict']['subdict2'] = {'this': 'is what i want'}
from collections import defaultdict
mydict = defaultdict(dict)
mydict['subdict']['subdict2'] = {'this': 'is what i want'}
冷清清 2025-01-23 04:22:06

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

这里;

my_dict = {}
my_dict["nested_item_1"] = {}
my_dict["nested_item_1"] = your_variable_here

I would create an empty dict inside the dict in order to have nested dict.

Here;

my_dict = {}
my_dict["nested_item_1"] = {}
my_dict["nested_item_1"] = your_variable_here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文