我有一本字典,看起來大致是這樣的:如何比較字典中的值?
{'METTS MARK': {'salary': 365788, 'to_messages': 807, 'deferral_payments': 'NaN', 'total_payments': 1061827, 'exercised_stock_options': 'NaN', 'bonus': 600000, 'restricted_stock': 585062, 'shared_receipt_with_poi': 702, 'restricted_stock_deferred': 'NaN', 'total_stock_value': 585062, 'expenses': 94299, 'loan_advances': 'NaN', 'from_messages': 29, 'other': 1740, 'from_this_person_to_poi': 1, 'poi': False, 'director_fees': 'NaN', 'deferred_income': 'NaN', 'long_term_incentive': 'NaN', 'email_address': '[email protected]', 'from_poi_to_this_person': 38},
'BAXTER JOHN C': {'salary': 267102, 'to_messages': 'NaN', 'deferral_payments': 1295738, 'total_payments': 5634343, 'exercised_stock_options': 6680544, 'bonus': 1200000, 'restricted_stock': 3942714, 'shared_receipt_with_poi': 'NaN', 'restricted_stock_deferred': 'NaN', 'total_stock_value': 10623258, 'expenses': 11200, 'loan_advances': 'NaN', 'from_messages': 'NaN', 'other': 2660303, 'from_this_person_to_poi': 'NaN', 'poi': False, 'director_fees': 'NaN', 'deferred_income': -1386055, 'long_term_incentive': 1586055, 'email_address': 'NaN', 'from_poi_to_this_person': 'NaN'},
'ELLIOTT STEVEN': {'salary': 170941, 'to_messages': 'NaN', 'deferral_payments': 'NaN', 'total_payments': 211725, 'exercised_stock_options': 4890344, 'bonus': 350000, 'restricted_stock': 1788391, 'shared_receipt_with_poi': 'NaN', 'restricted_stock_deferred': 'NaN', 'total_stock_value': 6678735, 'expenses': 78552, 'loan_advances': 'NaN', 'from_messages': 'NaN', 'other': 12961, 'from_this_person_to_poi': 'NaN', 'poi': False, 'director_fees': 'NaN', 'deferred_income': -400729, 'long_term_incentive': 'NaN', 'email_address': '[email protected]', 'from_poi_to_this_person': 'NaN'}
}
這是字典的只是一小部分,但。我將如何使用for循環來遍歷每個子字典的薪水值,並將其與下一個比較,以找出誰的薪水最高?我正在嘗試這樣的事情:
big = 0
for i in data_dict:
if data_dict[i]["salary"] > big:
big = i
print i
雖然它沒有給我正確的答案。另外,我將如何使用for循環來檢查誰擁有最高的薪水和最大的獎金?任何幫助將不勝感激。謝謝。
'大= i' <=你存儲密鑰(即名稱),而不是年薪值。 print i打印迭代過程中最後一個值,即字典中的最後一個鍵。 – dhke
很高興看到你真的想做點什麼。這是這裏初學者非常鼓勵的那類問題。 –