# (== get a representation sorted by value)
>>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
>>> sorted(xs.items(), key=lambda x: x[1])
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
# Or:
>>> import operator
>>> sorted(xs.items(), key=operator.itemgetter(1))
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]

如果要变成dict形式,可以使用以下

import collections

sorted_dict = collections.OrderedDict(sorted_x)

参考来源:https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

相关文章:

  • 2021-06-16
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2022-01-17
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-25
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2022-01-10
相关资源
相似解决方案