在 Python 中更新和创建多维字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14867496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:42:34  来源:igfitidea点击:

Update and create a multi-dimensional dictionary in Python

pythondictionarymultidimensional-array

提问by gregwhitworth

I am parsing JSON that stores various code snippets and I am first building a dictionary of languages used by these snippets:

我正在解析存储各种代码片段的 JSON,我首先构建了这些片段使用的语言字典:

snippets = {'python': {}, 'text': {}, 'php': {}, 'js': {}}

Then when looping through the JSON I'm wanting add the information about the snippet into its own dictionary to the dictionary listed above. For example, if I had a JS snippet - the end result would be:

然后,当循环遍历 JSON 时,我想将有关片段的信息添加到其自己的字典中,以添加到上面列出的字典中。例如,如果我有一个 JS 片段 - 最终结果将是:

snippets = {'js': 
                 {"title":"Script 1","code":"code here", "id":"123456"}
                 {"title":"Script 2","code":"code here", "id":"123457"}
}

Not to muddy the waters - but in PHP working on a multi-dimensional array I would just do the following (I am lookng for something similiar):

不要搅浑水 - 但在处理多维数组的 PHP 中,我只会执行以下操作(我正在寻找类似的东西):

snippets['js'][] = array here

I know I saw one or two people talking about how to create a multidimensional dictionary - but can't seem to track down adding a dictionary to a dictionary within python. Thanks for the help.

我知道我看到一两个人在谈论如何创建多维字典 - 但似乎无法追踪在 python 中将字典添加到字典中。谢谢您的帮助。

采纳答案by JBernardo

This is called autovivification:

这称为自动活化

You can do it with defaultdict

你可以用 defaultdict

def tree():
    return collections.defaultdict(tree)

d = tree()
d['js']['title'] = 'Script1'

If the idea is to have lists, you can do:

如果想法是有列表,你可以这样做:

d = collections.defaultdict(list)
d['js'].append({'foo': 'bar'})
d['js'].append({'other': 'thing'})

The idea for defaultdict it to create automatically the element when the key is accessed. BTW, for this simple case, you can simply do:

defaultdict 的想法是在访问键时自动创建元素。顺便说一句,对于这个简单的情况,您可以简单地执行以下操作:

d = {}
d['js'] = [{'foo': 'bar'}, {'other': 'thing'}]

回答by placeybordeaux

From

snippets = {'js': 
                 {"title":"Script 1","code":"code here", "id":"123456"}
                 {"title":"Script 2","code":"code here", "id":"123457"}
}

It looks to me like you want to have a list of dictionaries. Here is some python code that should hopefully result in what you want

在我看来,您想要一个字典列表。这是一些 python 代码,希望能产生你想要的结果

snippets = {'python': [], 'text': [], 'php': [], 'js': []}
snippets['js'].append({"title":"Script 1","code":"code here", "id":"123456"})
snippets['js'].append({"title":"Script 1","code":"code here", "id":"123457"})
print(snippets['js']) #[{'code': 'code here', 'id': '123456', 'title': 'Script 1'}, {'code': 'code here', 'id': '123457', 'title': 'Script 1'}]

Does that make it clear?

这样说清楚了吗?