繁体   English   中英

访问 Python 字典中的嵌套键值

[英]Access nested key-values in Python dictionary

我有以下字典结构,我正在尝试访问total_payments字段。 这就像访问 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}}

很难读你的字典,但有一个例子:

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

#prints 2

如果您的词典有两个子词典:

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

在你的情况下:

value['total_payments']

#returns 1061827

似乎您的 dictay 的值是 dict,而不是键是 dict。 所以你可以按如下方式访问:

your_dict[KEY1]['total_payments']

还请匿名您示例中的电子邮件地址。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM