【python】dictオブジェクトをkeysでソート
以下コード例
import collections dct = {2:3, 1:89, 4:5, 3:0} new_dict = collections.OrderedDict(sorted(dct.items())) new_dict # => OrderedDict([(1, 89), (2, 3), (3, 0), (4, 5)]) new_dict.keys() # => odict_keys([1, 2, 3, 4]) new_dict.values() # => odict_values([89, 3, 0, 5]) new_dict.items() # => odict_items([(1, 89), (2, 3), (3, 0), (4, 5)])