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. I tried key=sum(operator.itemgetter(1))
but it's clear that it won't work, I am getting:
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.
>>> 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:
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
.
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)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.