用Python创建2D字典 [英] Creating 2D dictionary in Python
本文介绍了用Python创建2D字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从"set1"的输出中有一个详细列表,例如名称",位置",动物",事物"以及具有相同详细信息的"set2".
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.
我想在这些行上使用 dict_names [setx] ['name'] ...
等创建字典.
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?
我不确定2D在字典中如何工作.任何指针吗?
I am not sure how 2D works in dictionary.. Any pointers?
推荐答案
它将具有以下语法
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屋!
查看全文