html tool

2016年10月28日星期五

dist 按key sort

问题:如何对dist按key排序

方案: 参考:http://stackoverflow.com/questions/9001509/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)])

没有评论:

发表评论