Arden Irving
Arden Irving

Reputation: 31

How do I compare values in a dictionary?

I have a dictionary that looks roughly like this:

{'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'}
}

That's just a small fraction of the dictionary though. How would I go about using a for loop to cycle through the salary values of each sub-dictionary, and compare it to the next, to find out who has the biggest salary? I was trying something like this:

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

It's not giving me the correct answer though. Also, how would I go about using the for loop to check who has the biggest salary AND the biggest bonus? Any help would be greatly appreciated. Thank you.

Upvotes: 3

Views: 2618

Answers (5)

Carlo
Carlo

Reputation: 835

Try this:

big = 0

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

Remember that an array or objects is like this:

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

Upvotes: 0

Stefan Gruenwald
Stefan Gruenwald

Reputation: 2640

You can do it the following way:

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

Upvotes: 0

boot-scootin
boot-scootin

Reputation: 12515

You can pare down the large dictionary you have into just name: salary key-value pairs, if you're just looking to find out who has the greatest salary and you don't care about some of the other attributes (although it's not to hard to use the smaller dictionary lookup to maneuver the larger dictionary).

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)

Upvotes: 0

dvdwllc
dvdwllc

Reputation: 66

Your problem is that, after the first loop, you're comparing the current person's salary to the name of the previous person. You should be more explicit in your naming convention to avoid mistakes like this. For example, rather than referring to the keys in your data_dict as "i," you should give them a descriptive name in your for: loop. Modifying your code a bit will produce the correct result:

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)

This assumes that the salary will always be a float or an integer. You can add additional placeholders and comparisons to determine who has the highest bonus as well.

Upvotes: 0

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

your original error was to store the wrong data as max instead of the salary value.

You can compute the maximum more eficiently and "pythonic" using max on the dictionary using a key function which is a tuple salary/bonus (so same salary: compares on bonus):

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

this gives me

METTS MARK

Upvotes: 7

Related Questions