一方面,您没有对 adict
进行排序 - 您正在对列表进行排序dicts
- 这些是非常不同的事情,尤其是因为dict
Python 中的 a 没有定义的顺序。
您可以使用sorted()
和轻松做到这一点operator.itemgetter()
:
import operator
sorted_dicts = sorted(dicts, key=operator.itemgetter("diff"))
sorted()
内置函数接受关键字参数,这key
是一个接受值的函数,并给出另一个值进行排序。在这里,我们使用 an从排序依据itemgetter()
中获取所需的值。dict
编辑:
鉴于您的更改,有两个答案,因为您不清楚。如果您想要值列表,只需从原始值中提取它们dict
:
sorted(users.values(), key=operator.itemgetter("diff"))
这就像服用一样简单dict.values()
。自然,在 Python 2.x 下,您会想要使用viewitems()
oriteritems()
以获得良好的性能。
如果您想对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>']
要产生一个 sorted 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
key 参数使用声明,因为itemgetter()
不幸的是无法为我们获取多个级别的项目。