用Python创建2D字典
我有一个“set1”输出的详细列表,如“name”、“place”、“animal”、“thing” 和一个具有相同细节的“set2” 我想创建一个字典,在这些行上有用Python创建2D字典,python,dictionary,set,key,2d,Python,Dictionary,Set,Key,2d,我有一个“set1”输出的详细列表,如“name”、“place”、“animal”、“thing” 和一个具有相同细节的“set2” 我想创建一个字典,在这些行上有dict_names[setx]['name']…等 这是最好的方法吗?如果不是,我该怎么做 我不知道2D在字典里是怎么用的。。有指针吗?它将具有以下语法 dict_names = {'d1' : {'name':'bob', 'place':'lawn', 'animal':'man'}, 'd2' :
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']
'bob'
类似这样的方法会奏效:
set1 = {
'name': 'Michael',
'place': 'London',
...
}
# same for set2
d = dict()
d['set1'] = set1
d['set2'] = set2
然后你可以做:
d['set1']['name']
等等。最好将其视为嵌套结构(而不是2D矩阵):
寻找一种可视化嵌套字典的简单方法。类似的方法应该可以工作
dictionary = dict()
dictionary[1] = dict()
dictionary[1][1] = 3
print(dictionary[1][1])
您还可以将其扩展到更高的维度 这将有助于给出一个更具体、更完整的例子。
setx
中有什么,您希望dict_names[setx]['name']返回什么?我创建了{'name':'bob','place':'lawn','animal':'man'}部分,但如何将其指定为d1的键?dict_names['d1']={'name':'bob','place:'lawn','animal':'man'}
这就像是一本普通的1D字典。对不起,我把整本书都看完了。我只花了几分钟,奇怪的是,尽管我清楚地理解了字典的概念。我觉得那些大建筑有点吓坏我了!如果我需要通过第一个键和第二个键来获取顶层,例如,拉动所有包含“name”标记的对象,这会保存重复的值
dictionary = dict()
dictionary[1] = dict()
dictionary[1][1] = 3
print(dictionary[1][1])