【发布时间】:2018-02-19 10:13:01
【问题描述】:
我有一本 python 字典:
d = {'a1': 123, 'a2': 2, 'a10': 333, 'a11': 4456}
当我使用 OrderedDict 对字典进行排序时,我得到以下输出:
from collections import OrderedDict
OrderedDict(sorted(d.items()))
# Output
# OrderedDict([('a1', 123), ('a10', 333), ('a11', 4456), ('a2', 2)])
有没有办法按自然顺序获取:
OrderedDict([('a1', 123), ('a2', 2), ('a10', 333), ('a11', 4456)])
or
{'a1': 123, 'a2': 2, 'a10': 333, 'a11': 4456}
谢谢。
【问题讨论】:
标签: python sorting dictionary