字典列表中的Python访问字典 [英] Python access dictionary inside list of a dictionary

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

问题描述

我有下面的字典,它带有一个带有列表的值,并且列表内是一个字典.有没有一种方法可以使用键而不是列表索引来调用列表内的字典值?列表中的字典可能会有所不同,因此索引值可能并不总是提供正确的键值对.但是,如果我能够使用密钥,那么我总是可以得到正确的值.

Hi I have the below dictionary which has a value with a list, and inside the list is a dictionary. Is there a way to call the dictionary value inside the list using the key instead of the list index? The dictionary inside the list may vary so the index value may not always provide the right key value pair. But if I am able to use the key I can always get the correct value.

mylist = {'mydict': [{'A': 'Letter A'}, {'B': 'Letter C'}, {'C': 'Letter C'}]}
print(mylist['mydict'][0]['A'])

当前输出:

Letter A

所需查询:

print(mylist['mydict']['A'])
Letter A

推荐答案

目前,字典中的列表中有3个字典.请尝试以下操作:

At the moment you have 3 dictionaries inside a list inside a dictionary. Try the below instead:

my_nested_dictionary = {'mydict': {'A': 'Letter A', 'B': 'Letter C', 'C': 'Letter C'}}
print(my_nested_dictionary['mydict']['A'])

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

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