在 Python 中创建 2D 字典 [英] Creating 2D dictionary in Python

查看:34
本文介绍了在 Python 中创建 2D 字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个set1"输出的详细信息列表,比如name"、place"、animal"、thing"和具有相同细节的set2".

我想用 dict_names[setx]['name']... 等在这些行上创建一个字典.

这是最好的方法吗?如果不是我该怎么做?

我不确定字典中的 2D 是如何工作的.. 任何指针?

解决方案

它将具有以下语法

dict_names = {'d1' : {'name':'bob', 'place':'lawn', 'animal':'man'},'d2' : {'name':'spot', 'place':'bed', 'animal':'dog'}}

然后你可以像这样查找

<预><代码>>>>dict_names['d1']['name']'鲍勃'

I have a list of details from an output for "set1" which are like "name", "place", "animal", "thing" and a "set2" with the same details.

I want to create a dictionary with dict_names[setx]['name']... etc On these lines.

Is that the best way to do it? If not how do I do it?

I am not sure how 2D works in dictionary.. Any pointers?

解决方案

It would have the following syntax

dict_names = {'d1' : {'name':'bob', 'place':'lawn', 'animal':'man'},
              'd2' : {'name':'spot', 'place':'bed', 'animal':'dog'}}

You can then look things up like

>>> dict_names['d1']['name']
'bob'

这篇关于在 Python 中创建 2D 字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆