2017-02-13 86 views
3

我有一本字典,看起來大致是這樣的:如何比較字典中的值?

{'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循環來檢查誰擁有最高的薪水和最大的獎金?任何幫助將不勝感激。謝謝。

+0

'大= i' <=你存儲密鑰(即名稱),而不是年薪值。 print i打印迭代過程中最後一個值,即字典中的最後一個鍵。 – dhke

+0

很高興看到你真的想做點什麼。這是這裏初學者非常鼓勵的那類問題。 –

回答

7

你最初的錯誤是將錯誤的數據存儲爲max而不是工資值。

可以計算最大更加eficiently和「Python的」關於使用key函數,它是一個元組的工資/獎金字典使用max(所以同樣的薪水:比較了獎金):

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

這給我

METTS MARK 
+1

再次,好的工作總結了一些事情,但可能想告訴他,他正在重新分配一本字典,並且仍然試圖將其與數字進行比較.... – MooingRawr

+0

你是對的。我對pythonic的東西太多了:)稍作編輯。 –

0

試試這個:

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}] 
+0

'TypeError:字符串索引必須是整數'你基本上和OP做同樣的錯誤....你正在分配一個大的字典值,並仍然期望它與一個int比較...... – MooingRawr

+0

將int添加到你的字符串解決問題 – Carlo

0

您的問題是,在第一循環之後,你當前人的工資比較以前的人的名字。你應該在你的命名約定中更加明確,以避免這樣的錯誤。例如,不要將data_dict中的鍵稱爲「我」,而應該在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) 

這假設薪水永遠是浮點數或整數。您可以添加額外的佔位符和比較來確定誰擁有最高獎金。

+0

我相信這是最符合我想要做的。這可能比其他答案更復雜,但是,我覺得試着以這種方式解決它可以幫助我更好地掌握語法(如果有人認爲這不值得(或有害),請讓我知道。應該提到並非所有的值都是整數或浮點數,其中一些值等於「NaN」,我將如何處理這個問題?我試過了「如果data_dict [name] ['salary]> high_salary和!= 「NaN」:「,但它會引發語法錯誤,將它嵌套爲if語句會給出正確的值,但是錯誤的人 –

+0

沒關係,我通過將print中的」name「更改爲」high_salary_holder「 。 十分感謝你的幫助! –

0

可以削減下來你逼到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) 
0

可以做到這一點通過以下方式:

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