2016-10-04 71 views
-3

我有以下字典結構和Iam試圖訪問total_payments字段。這就像在Python字典訪問鍵的鍵:訪問Python字典中的嵌套鍵值

d = {'METTS MARK': {'bonus': 600000, 
      'deferral_payments': 'NaN', 
      'deferred_income': 'NaN', 
      'director_fees': 'NaN', 
      'email_address': '[email protected]', 
      '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}} 
+0

'your_dict ['METTS MARK'] ['total_payments']' –

回答

1

它似乎是你的dictionay的價值是字典,而不是鑰匙是字典。 所以,你可以訪問如下:

your_dict[KEY1]['total_payments'] 

也請在你的榜樣匿名的電子郵件地址。

1

很難讀你的字典,但有一個例子:

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