加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何比较字典中的值?

发布时间:2020-12-20 11:57:54 所属栏目:Python 来源:网络整理
导读:我有一个看起来像这样的字典: {'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': 70
我有一个看起来像这样的字典:

{'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','total_stock_value': 10623258,'expenses': 11200,'from_messages': 'NaN','other': 2660303,'from_this_person_to_poi': 'NaN','deferred_income': -1386055,'long_term_incentive': 1586055,'email_address': 'NaN','from_poi_to_this_person': 'NaN'},'ELLIOTT STEVEN': {'salary': 170941,'total_payments': 211725,'exercised_stock_options': 4890344,'bonus': 350000,'restricted_stock': 1788391,'total_stock_value': 6678735,'expenses': 78552,'other': 12961,'deferred_income': -400729,'email_address': 'steven.elliott@enron.com','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循环来检查谁拥有最高薪水和最大奖金?任何帮助将不胜感激.谢谢.

解决方法

您的原始错误是将错误的数据存储为max而不是工资值.

你可以使用关键函数使用字典上的max来更有效地计算最大值和“pythonic”,这是一个元组工资/奖金(所以相同的工资:奖金比较):

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

这给了我

METTS MARK

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读