日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python如何使用字典中的值并进行比较_python – 如何比较字典中的值?

發(fā)布時(shí)間:2025/4/5 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何使用字典中的值并进行比较_python – 如何比较字典中的值? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我有一個(gè)看起來(lái)像這樣的字典:

{'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': 'mark.metts@enron.com', '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': 'steven.elliott@enron.com', 'from_poi_to_this_person': 'NaN'}

}

這只是字典的一小部分.我將如何使用for循環(huán)遍歷每個(gè)子詞典的工資值,并將其與下一個(gè)詞典進(jìn)行比較,以找出誰(shuí)擁有最高薪水?我正在嘗試這樣的事情:

big = 0

for i in data_dict:

if data_dict[i]["salary"] > big:

big = i

print i

但它并沒(méi)有給我正確答案.另外,我如何使用for循環(huán)來(lái)檢查誰(shuí)擁有最高薪水和最大獎(jiǎng)金?任何幫助將不勝感激.謝謝.

最佳答案 您的原始錯(cuò)誤是將錯(cuò)誤的數(shù)據(jù)存儲(chǔ)為max而不是工資值.

你可以使用關(guān)鍵函數(shù)使用字典上的max來(lái)更有效地計(jì)算最大值和“pythonic”,這是一個(gè)元組工資/獎(jiǎng)金(所以相同的工資:獎(jiǎng)金比較):

print(max(d,key=lambda x : (d[x]["salary"],d[x]["bonus"])))

這給了我

METTS MARK

總結(jié)

以上是生活随笔為你收集整理的python如何使用字典中的值并进行比较_python – 如何比较字典中的值?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。