按值(列表)对字典进行排序

时间:2015-12-03 21:54:49

标签: python python-3.x 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],
}

我想按每个列表的总和对字典进行排序。所以我可以这样做:

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()这么多次。
  3. 我试过了key=sum(operator.itemgetter(1)),但很清楚它不会工作,我得到了:

    TypeError: 'operator.itemgetter' object is not iterable
    

    什么是更有效的方式来做我想要的?

4 个答案:

答案 0 :(得分: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

CMAKE_FIND_ROOT_PATH

答案 1 :(得分:3)

他们的密钥需要是calalble,所以你需要将你的操作包装在一个函数中:

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

答案 2 :(得分: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])])

答案 3 :(得分: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)]