按分數對字典進行排序。如果分數相同,則通過名稱對其進行排序如果數值相同,如何對字典中的鍵進行排序?
import operator
dt={
'sudha' : {'score' : 75} ,
'Amruta' : {'score' : 95} ,
'Ramesh' : {'score' : 56} ,
'Shashi' : {'score' : 78} ,
'Manoj' : {'score' : 69} ,
'Resham' : {'score' : 95}
}
sorted_x1 = sorted(dt.items(), key=operator.itemgetter(1))
print sorted_x1
輸出爲:
[('Ramesh', {'score': 56}), ('Manoj', {'score': 69}),
('sudha', {'score': 75}),
('Shashi', {'score': 78}), ('Resham', {'score': 95}), ('Amruta',{'score': 95})]
現在我想排序由其名稱最後兩個元素,因爲它的分數是一樣的。
'鍵= operator.itemgetter(1,0)' –
@AshwiniChaudhary,再看一遍。 'itemgetter(1)'是從 –
@JohnLaRooy Ouch建立起的一個糟糕的起點! –