在Python的字典中创建/附加嵌套字典? [关闭]

问题描述 投票:-2回答:1

如何附加字典?我尝试了maindict[a] = mmaindict[a][x] = n。它没有用,我得到TypeError: list indices must be integers or slices, not str

maindict = 
{ 'a' : '',
  'b' : '',
...
}

m = 
{ 'x' : '', 
  'y' : ''}

n = 
{ 'l' : '', 
  'm' : ''}

tobe

maindict =  { 'a' : { 'x' : { 'l' : '', 
  'm' : ''}, 'y' : ''},   
'b' : '', ... }
python python-3.x
1个回答
1
投票

您只需要识别您的字典键是字符串文字,而不是变量。

maindict['a'] = m
maindict['a']['x'] = n
© www.soinside.com 2019 - 2024. All rights reserved.