繁体   English   中英

如何比较字典中的值?

[英]How do I compare values in a dictionary?

我有一个看起来像这样的字典:

{'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循环遍历每个子词典的工资值,并将其与下一个词典进行比较,以找出谁拥有最高薪水? 我正在尝试这样的事情:

big = 0
for i in data_dict:
    if data_dict[i]["salary"] > big:
        big = i
print i 

但它并没有给我正确答案。 另外,我如何使用for循环来检查谁拥有最高薪水和最大奖金? 任何帮助将不胜感激。 谢谢。

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

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

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

这给了我

METTS MARK

试试这个:

big = 0

    for i in data_dict:
        if int(i['salary']) > big:
            big = i
    print i 

请记住,一个或多个对象是这样的:

['Person': {a:5,b:6},'Person': {a:3,b:6}]

你的问题是,在第一个循环之后,你将当前人的工资与前一个人的名字进行比较。 您应该在命名约定中更明确,以避免这样的错误。 例如,您不应将data_dict中的键称为“i”,而应在for:循环中为它们指定一个描述性名称。 稍微修改一下代码会产生正确的结果:

high_salary = 0
high_salary_holder = ''
for name in data_dict:
    if data_dict[name]['salary'] > high_salary:
        high_salary = data_dict[name]['salary']
        high_salary_holder = name
print name, str(high_salary)

这假设薪水总是浮点数或整数。 您可以添加其他占位符和比较,以确定谁具有最高奖金。

你可以把你所拥有的大字典削减到name: salary键值对, 如果你只是想找出谁有最高的工资而你不关心其他一些属性(虽然它不是难以使用较小的字典查找来操纵较大的字典)。

just_salary = {name: d[name]['salary'] for name in d}

just_salary
Out[143]: {'BAXTER JOHN C': 267102, 'ELLIOTT STEVEN': 170941, 'METTS MARK': 365788}

# Give me the tuple for which salary (the second item) is greatest
max(just_salary.items(), key = lambda x: x[1])
Out[144]: ('METTS MARK', 365788)

您可以通过以下方式执行此操作:

my_dict = {
'METTS MARK': {'salary': 365788,'age': 32},
'ELLIOTT STEVEN': {'salary': 170941,'age': 54},
'TRUMP DONALD': {'salary': 10000,'age': 70}}

sorted_dict = sorted(my_dict.iteritems(), key=lambda x: x[1]['salary'])

for key,value in sorted_dict:
    if value['salary']>100000:
        print value

暂无
暂无

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

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