简体   繁体   English

访问 Python 字典中的嵌套键值

[英]Access nested key-values in Python dictionary

I have the following dictionary structure and Iam trying to access the total_payments field.我有以下字典结构,我正在尝试访问total_payments字段。 It is like accessing key of keys in Python dictionary:这就像访问 Python 字典中的键的键:

d = {'METTS MARK': {'bonus': 600000,
            'deferral_payments': 'NaN',
            'deferred_income': 'NaN',
            'director_fees': 'NaN',
            'email_address': 'a@b.com',
            'exercised_stock_options': 'NaN',
            'expenses': 94299,
            'from_messages': 29,
            'from_poi_to_this_person': 38,
            'from_this_person_to_poi': 1,
            'loan_advances': 'NaN',
            'long_term_incentive': 'NaN',
            'other': 1740,
            'poi': False,
            'restricted_stock': 585062,
            'restricted_stock_deferred': 'NaN',
            'salary': 365788,
            'shared_receipt_with_poi': 702,
            'to_messages': 807,
            'total_payments': 1061827,
            'total_stock_value': 585062}}

Hard to read your dictionary, but there's an example:很难读你的字典,但有一个例子:

dic = {'abc': {'123': '2'}}
print(dic['abc']['123'])

#prints 2

and if your dictionary had two sub dictionaries:如果您的词典有两个子词典:

dic = {'abc': [{'123': '2'}, {'456': '4'}]}
print(dic['abc'][1]['456'])
# prints 4

in your case:在你的情况下:

value['total_payments']

#returns 1061827

It rather seems that values of your dictionay are dict, and not that the keys are dict.似乎您的 dictay 的值是 dict,而不是键是 dict。 So you can just access as follows:所以你可以按如下方式访问:

your_dict[KEY1]['total_payments']

Please also anonymize the email address in your example.还请匿名您示例中的电子邮件地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何访问列表中具有特定键值的字典 - Python: How to access the dictionary that has specific key-values in a list 将python字典的键值转换为pandas数据框 - Converting key-values of python dictionary into pandas dataframe 反转从文本文件读取的字典的键值-python - Reversing key-values for dictionary read from textfile - python 如何在django中创建字典键值 - How to create dictionary key-values in django 将 python 字典中的值推送到第二个字典的相应键值 - push values from a python dictionary to corresponding key-values of a second dictionary 如何有效地找到python中二维字典的存在的键值在4个值之间? - How to efficient find existent key-values of 2-dimensional dictionary in python which are between 4 values? 如何将 append 一个 python 字典转换为另一个字典,同时保持其键值不变? - How to append one python dictionary to another while preserving their key-values as it is? 如何使用变量访问嵌套python词典中的键值? - How to access the key values in a nested python dictionary with variables? 在字典中分离键值,并在处理值后重新创建字典 - Separate key-values in a dictionary and recreate the dict after processing the values 比较字典中的键值作为每天的用电量 - Comparing key-values in a dictionary as electrical usage per day
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM