Reorder dictionary in python according to a list of values

后端 未结 6 1178
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 04:27

Let us consider a dictionary:

sample_dict={1:\'r099\',2:\'g444\',3:\'t555\',4:\'f444\',5:\'h666\'}

I want to re-order this dictionary in an

6条回答
  •  春和景丽
    2021-01-12 04:46

    Use SortedDict provided by django (from django.utils.datastructures import SortedDict). SortedDict stores it's order in keyOrder attribute (which is just a list, so you can reorder it any way you like, anytime).

    If you don't have django installed or have no use for django, just lift the implementation django.utils.datatstructures. It doesn't depend on any other parts of django.

提交回复
热议问题