Python 3 sort a dict by its values

后端 未结 6 1307
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 14:20

The only methods I found work for python2 or return only list of tuples.

Is it possible to sort dictionary, e.g. {\"aa\": 3, \"bb\": 4, \"cc\": 2, \"dd\": 1}

6条回答
  •  日久生厌
    2020-12-07 15:08

    To sort a dictionary and keep it functioning as a dictionary afterwards, you could use OrderedDict from the standard library.

    If that's not what you need, then I encourage you to reconsider the sort functions that leave you with a list of tuples. What output did you want, if not an ordered list of key-value pairs (tuples)?

提交回复
热议问题