How to sort dictionaries by keys in Python

后端 未结 7 1826
广开言路
广开言路 2020-12-15 05:28

Can anyone tell me how I can sort this:

{\'a\': [1, 2, 3], \'c\': [\'one\', \'two\'], \'b\': [\'blah\', \'bhasdf\', \'asdf\'], \'d\': [\'asdf\', \'wer\', \'a         


        
7条回答
  •  有刺的猬
    2020-12-15 06:26

    It may also be worth mentioning the nlargest routine in heapq. This sorts and returns the top N items. Depending upon what is actually required, this may be handy if you play with the key parameter. I mainly mention this since I discovered it a couple of nights ago and it did exactly what I was after. See PEP 0265 and Heapq.

提交回复
热议问题