python3字典取值问题?

python3字典取值问题?

list = {'a':{'b':1},'c':[{'d':2,'e':{'f':3,'msg':'测试'}},{'g':4}]}

print(list['c'][1])

for i in list['c']:
print(i['d'])
我是python菜鸟才开始学习碰到一个问题,想问一下大家上面这个字典集,我想取d的值,还有msg和g的值应该怎么编写代码,谢谢啦

https://blog.csdn.net/q5841818/article/details/80551413

list = {'a':{'b':1},'c':[{'d':2,'e':{'f':3,'msg':'测试'}},{'g':4}]}
#取d的值,还有msg和g的值

#首先将这个列表取出来
print(list.get('c'))

#得到结果 [{'d': 2, 'e': {'f': 3, 'msg': '测试'}}, {'g': 4}]
#可以看到集合[] 里面装了3个元素 0,1,2

#取出第一个元素 {'d': 2, 'e': {'f': 3, 'msg': '测试'}}
first = list.get('c')[0]
print(list.get('c')[0])
#获取d的对应的值 2
print(first.get('d'))
#先获取e对应的值 {'f': 3, 'msg': '测试'}
second = first.get('e')
print(second)
#再获取msg对应的值 测试
print(second.get('msg'))



#取出第三个元素 {'g': 4}
print(list.get('c')[1])
#获取g对应的值 4
print(list.get('c')[1].get('g'))

ps:字典不能索引,列表能够