190122 Python如何排序字典

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_33039859/article/details/86596219

How can I sort a dictionary by key?

In [1]: import collections

In [2]: d = {2:3, 1:89, 4:5, 3:0}

In [3]: od = collections.OrderedDict(sorted(d.items()))

In [4]: od
Out[4]: OrderedDict([(1, 89), (2, 3), (3, 0), (4, 5)])

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33039859/article/details/86596219
今日推荐