[英]Sort a dictionary by value(list) applying function to it
I have a dictionary: 我有一本字典:
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],
}
I want to sort the dictionary by sum of each list. 我想按每个列表的总和对字典进行排序。 So I could do it like this: 所以我可以这样:
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)
But: 但:
sum()
so many times. 我不想写多次sum()
。 I tried key=sum(operator.itemgetter(1))
but it's clear that it won't work, I am getting: 我尝试了key=sum(operator.itemgetter(1))
但是很明显它不起作用,我得到了:
TypeError: 'operator.itemgetter' object is not iterable
What is more efficient way to do what I want? 有什么更有效的方式来做我想做的事?
dictionaries are not ordered, therefore it is impossible to "sort your dictionary". 字典未排序,因此无法对字典进行“排序”。 If you want,however, a separate list of keys that are ordered, this is doable: 但是,如果需要一个单独的已排序键列表,则可以这样做:
>>> 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']
you can then iterate through the list as you would iterate through a dict
since iter(dict)
iterates through the keys of dict
anyways. 那么你可以遍历列表,你会通过遍历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
它们的键必须是可操作的,因此您需要将操作包装在一个函数中:
key = lambda x: sum(x[1])
If you want order based on highest sum use an OrderedDict
after sorting by sum: 如果要基于最高总和订购,请在按总和排序后使用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])])
If you want it sorted from highest to lowest sum use -sum(...
in the sorted
. 如果要从最高和最低sorted
,请在sorted
使用-sum(...
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])])
Since it's not possible to sort a dictionary, we can only get a representation of a dict that is sorted which will be a list, actually a list of tuples. 由于无法对字典进行排序,因此我们只能得到排序后的字典的表示形式,该字典将是一个列表,实际上是一个元组列表。
d = {k: sum(i for i in v) for k,v in students.items()}
print (sorted(d.items(), key=operator.itemgetter(1)))
Output: 输出:
[('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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.