如何按其值排序python dict?

时间:2012-05-12 02:50:02

标签: python sorting dictionary

最初的数据是:

dict = {<User: user2>: {'diff': 48, 'alike': 1}, <User: user3>: {'diff': 42, 'alike': 2}, <User: user4>: {'diff': 45, 'alike': 1}, <User: user5>: {'diff': 43, 'alike':
 2}, <User: user6>: {'diff': 46, 'alike': 1}, <User: user7>: {'diff': 46, 'alike': 1}, <User: user8>: {'diff': 49, 'alike': 1}, <User: user9>: {'diff': 50, 'ali
ke': 0}, <User: user10>: {'diff': 46, 'alike': 1}, <User: user11>: {'diff': 37, 'alike': 3}, <User: user12>: {'diff': 50, 'alike': 0}, <User: user13>: {'diff':
50, 'alike': 0}, <User: user14>: {'diff': 50, 'alike': 0}, <User: user15>: {'diff': 50, 'alike': 0}, <User: user16>: {'diff': 50, 'alike': 0}, <User: user17>: {
'diff': 50, 'alike': 0}, <User: user18>: {'diff': 50, 'alike': 0}, <User: user19>: {'diff': 50, 'alike': 0}, <User: user20>: {'diff': 50, 'alike': 0}}

然后我对它进行排序:

sorted(dict) == [{'diff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'d
    iff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'diff': 50, 'alike': 0}, {'diff': 45, 'alike': 1}, {'diff': 46, 'alike': 1}, {'diff'
    : 46, 'alike': 1}, {'diff': 46, 'alike': 1}, {'diff': 48, 'alike': 1}, {'diff': 49, 'alike': 1}, {'diff': 42, 'alike': 2}, {'diff': 43, 'alike': 2}, {'diff': 37
    , 'alike': 3}]

如何按“diff”排序?

2 个答案:

答案 0 :(得分:8)

首先,你没有对dict进行排序 - 你正在对dicts的列表进行排序 - 这些是非常不同的事情,尤其是因为Python中的dict没有定义的顺序

您可以使用sorted()operator.itemgetter()轻松完成此操作:

import operator
sorted_dicts = sorted(dicts, key=operator.itemgetter("diff"))

sorted()内置函数采用key关键字参数,该参数是一个取值的函数,并给出另一个值进行排序。在这里,我们使用itemgetter()dict中获取所需的值以进行排序。

编辑:

鉴于您的变化,有两个答案,因为您不清楚。如果您需要值列表,只需从原始dict中提取它们即可:

sorted(users.values(), key=operator.itemgetter("diff"))

这就像取dict.values()一样简单。当然,在Python 2.x下,您需要使用viewitems()iteritems()才能获得良好的性能。

如果你想对dict本身进行排序,那是另一回事,因为dict s(如我所说)本质上是无序的。

首先,我想指出sorted(dict)没有产生你建议的输出 - 一个dict迭代键,而不是默认值:

users = {
    '<User: user10>': {'alike': 1, 'diff': 46},
    '<User: user11>': {'alike': 3, 'diff': 37},
    '<User: user12>': {'alike': 0, 'diff': 50},
    '<User: user13>': {'alike': 0, 'diff': 50},
    '<User: user14>': {'alike': 0, 'diff': 50},
    '<User: user15>': {'alike': 0, 'diff': 50},
    '<User: user16>': {'alike': 0, 'diff': 50},
    '<User: user17>': {'alike': 0, 'diff': 50},
    '<User: user18>': {'alike': 0, 'diff': 50},
    '<User: user19>': {'alike': 0, 'diff': 50},
    '<User: user20>': {'alike': 0, 'diff': 50},
    '<User: user2>': {'alike': 1, 'diff': 48},
    '<User: user3>': {'alike': 2, 'diff': 42},
    '<User: user4>': {'alike': 1, 'diff': 45},
    '<User: user5>': {'alike': 2, 'diff': 43},
    '<User: user6>': {'alike': 1, 'diff': 46},
    '<User: user7>': {'alike': 1, 'diff': 46},
    '<User: user8>': {'alike': 1, 'diff': 49},
    '<User: user9>': {'alike': 0, 'diff': 50}
}

print(sorted(users))

给我们:

['<User: user10>', '<User: user11>', '<User: user12>', '<User: user13>', '<User: user14>', '<User: user15>', '<User: user16>', '<User: user17>', '<User: user18>', '<User: user19>', '<User: user20>', '<User: user2>', '<User: user3>', '<User: user4>', '<User: user5>', '<User: user6>', '<User: user7>', '<User: user8>', '<User: user9>']

要生成已排序的dict,我们需要使用collections.OrderedDict()

import collections

users = {
    '<User: user10>': {'alike': 1, 'diff': 46},
    '<User: user11>': {'alike': 3, 'diff': 37},
    '<User: user12>': {'alike': 0, 'diff': 50},
    '<User: user13>': {'alike': 0, 'diff': 50},
    '<User: user14>': {'alike': 0, 'diff': 50},
    '<User: user15>': {'alike': 0, 'diff': 50},
    '<User: user16>': {'alike': 0, 'diff': 50},
    '<User: user17>': {'alike': 0, 'diff': 50},
    '<User: user18>': {'alike': 0, 'diff': 50},
    '<User: user19>': {'alike': 0, 'diff': 50},
    '<User: user20>': {'alike': 0, 'diff': 50},
    '<User: user2>': {'alike': 1, 'diff': 48},
    '<User: user3>': {'alike': 2, 'diff': 42},
    '<User: user4>': {'alike': 1, 'diff': 45},
    '<User: user5>': {'alike': 2, 'diff': 43},
    '<User: user6>': {'alike': 1, 'diff': 46},
    '<User: user7>': {'alike': 1, 'diff': 46},
    '<User: user8>': {'alike': 1, 'diff': 49},
    '<User: user9>': {'alike': 0, 'diff': 50}
}

print(collections.OrderedDict(sorted(users.items(), key=lambda x: x[1]["diff"])))

这给了我们:

OrderedDict([('<User: user11>', {'diff': 37, 'alike': 3}), ('<User: user3>', {'diff': 42, 'alike': 2}), ('<User: user5>', {'diff': 43, 'alike': 2}), ('<User: user4>', {'diff': 45, 'alike': 1}), ('<User: user10>', {'diff': 46, 'alike': 1}), ('<User: user7>', {'diff': 46, 'alike': 1}), ('<User: user6>', {'diff': 46, 'alike': 1}), ('<User: user2>', {'diff': 48, 'alike': 1}), ('<User: user8>', {'diff': 49, 'alike': 1}), ('<User: user20>', {'diff': 50, 'alike': 0}), ('<User: user9>', {'diff': 50, 'alike': 0}), ('<User: user13>', {'diff': 50, 'alike': 0}), ('<User: user19>', {'diff': 50, 'alike': 0}), ('<User: user12>', {'diff': 50, 'alike': 0}), ('<User: user18>', {'diff': 50, 'alike': 0}), ('<User: user15>', {'diff': 50, 'alike': 0}), ('<User: user14>', {'diff': 50, 'alike': 0}), ('<User: user17>', {'diff': 50, 'alike': 0}), ('<User: user16>', {'diff': 50, 'alike': 0})])

请注意,我们必须在密钥参数中使用lambda语句,因为itemgetter()无法为我们获取多个级别的项目。

答案 1 :(得分:5)

sorted(your_list, key=lambda el: el["diff"])

key关键字参数应该是一个函数,它接受您正在排序的列表的元素,并返回应该在排序比较中使用的值。 lambdadef的简写 - 也就是说,它定义了一个函数(有几个限制)。可以用这种方式写出同一行:

def get_key(el):
    return el["diff"]

sorted(your_list, key=get_key)