2015-12-03 86 views
0

我有一本字典:排序一本詞典值(名單)賦予功能給它

students = { 
    '1': [32, 14, 31, 23], 
    '2': [32, 8, 36.5, 22], 
    '3': [26, 11, 39, 15.5], 
    '4': [34, 15, 44, 25], 
    '5': [30, 6, 25, 24], 
    '6': [26, 8, 31, 13], 
    '7': [24, 4, 16, 17], 
    '8': [22, 2, 0, 11.2], 
    '9': [22, 4, 15, 10], 
    '10': [31, 4, 16, 4.2], 
    '11': [18, 3.5, 0, 0], 
    '12': [28, 5, 30, 18.5], 
    '13': [34, 13, 23, 13], 
} 

我想每個列表的總和排序的字典。所以,我能做到這一點是這樣的:

import operator 

students = { 
    '1': sum([32, 14, 31, 23]), 
    '2': sum([32, 8, 36.5, 22]), 
    '3': sum([26, 11, 39, 15.5]), 
    '4': sum([34, 15, 44, 25]), 
    '5': sum([30, 6, 25, 24]), 
    '6': sum([26, 8, 31, 13]), 
    '7': sum([24, 4, 16, 17]), 
    '8': sum([22, 2, 0, 11.2]), 
    '9': sum([22, 4, 15, 10]), 
    '10': sum([31, 4, 16, 4.2]), 
    '11': sum([18, 3.5, 0, 0]), 
    '12': sum([28, 5, 30, 18.5]), 
    '13': sum([34, 13, 23, 13]), 
} 

sorted_students = sorted(
    students.items(), key=operator.itemgetter(1), reverse=True) 

但是:

  1. 我不想改變字典,我想有列爲值
  2. 我不想寫sum()這麼多次。

我試圖key=sum(operator.itemgetter(1))但很顯然,這是行不通的,我得到:

TypeError: 'operator.itemgetter' object is not iterable 

什麼是更有效的方式做我想做什麼?

回答

3

他們主要需要是calalble,所以你需要用一個函數的操作:

key = lambda x: sum(x[1]) 
4

字典是沒有順序的,所以這是不可能「排序你的字典」。如果你想,但是,排序鍵的單獨列表,這是可行的:

>>> students = { 
    '1': [32, 14, 31, 23], 
    '2': [32, 8, 36.5, 22], 
    '3': [26, 11, 39, 15.5], 
    '4': [34, 15, 44, 25], 
    '5': [30, 6, 25, 24], 
    '6': [26, 8, 31, 13], 
    '7': [24, 4, 16, 17], 
    '8': [22, 2, 0, 11.2], 
    '9': [22, 4, 15, 10], 
    '10': [31, 4, 16, 4.2], 
    '11': [18, 3.5, 0, 0], 
    '12': [28, 5, 30, 18.5], 
    '13': [34, 13, 23, 13], 
} 
>>> sortedKeys = sorted(students.keys(), key = lambda x: sum(students[x])) 
>>> sortedKeys 
['11', '8', '9', '10', '7', '6', '12', '13', '5', '3', '2', '1', '4'] 

然後你可以通過你將通過dict因爲iter(dict)遍歷的dict鍵遍歷反正對列表進行迭代。

>>> for key in sortedKeys: 
    print(key,sum(students[key])) 


11 21.5 
8 35.2 
9 51 
10 55.2 
7 61 
6 78 
12 81.5 
13 83 
5 85 
3 91.5 
2 98.5 
1 100 
4 118 
1

如果您想根據最高總和爲了使用OrderedDict的總和排序後:

from collections import OrderedDict 

students = OrderedDict(sorted(students.items(),key=lambda x: sum(x[1]))) 


OrderedDict([('11', [18, 3.5, 0, 0]), ('8', [22, 2, 0, 11.2]), 
      ('9', [22, 4, 15, 10]), ('10', [31, 4, 16, 4.2]), 
      ('7', [24, 4, 16, 17]), ('6', [26, 8, 31, 13]), 
      ('12', [28, 5, 30, 18.5]), ('13', [34, 13, 23, 13]), 
      ('5', [30, 6, 25, 24]), ('3', [26, 11, 39, 15.5]), 
      ('2', [32, 8, 36.5, 22]), ('1', [32, 14, 31, 23]), 
     ('4', [34, 15, 44, 25])]) 

如果你想從最高排序,以最低的總和使用-sum(...sorted

students = OrderedDict(sorted(students.items(),key=lambda x: -sum(x[1]))) 

print(students) 
OrderedDict([('4', [34, 15, 44, 25]), ('1', [32, 14, 31, 23]), 
     ('2', [32, 8, 36.5, 22]), ('3', [26, 11, 39, 15.5]), 
     ('5', [30, 6, 25, 24]), ('13', [34, 13, 23, 13]), 
     ('12', [28, 5, 30, 18.5]), ('6', [26, 8, 31, 13]), 
     ('7', [24, 4, 16, 17]), ('10', [31, 4, 16, 4.2]), 
     ('9', [22, 4, 15, 10]), ('8', [22, 2, 0, 11.2]), 
     ('11', [18, 3.5, 0, 0])]) 
1

因爲它是不可能的排序詞典,我們只能獲取的排序,這將是一個列表,實際上是一個元組列表的字典的表示。

d = {k: sum(i for i in v) for k,v in students.items()} 
print (sorted(d.items(), key=operator.itemgetter(1))) 

輸出:

[('11', 21.5), ('8', 35.2), ('9', 51), ('10', 55.2), ('7', 61), ('6', 78), ('12', 81.5), ('13', 83), ('5', 85), ('3', 91.5), ('2', 98.5), ('1', 100), ('4', 118)]