对字典排序
from collections import OrderedDict
test = {1: "a", 3: "d", 6: "g", 2: "c", 5: "e", 0: "f", 4:...'b'}
# 普通 dict 插入元素时是无序的,使用 OrderedDict 按元素插入顺序排序
# 对字典按key排序, 默认升序, 返回 OrderedDict
def sort_key(old_dict...([(0, 'f'), (1, 'a'), (2, 'c'), (3, 'd'), (4, 'b'), (5, 'e'), (6, 'g')])
OrderedDict([(1, 'a'), (4, '...简化使用 OrderedDict :
from collections import OrderedDict
test = {1: "a", 3: "d", 6: "g", 2: "c", 5: "e"..., 0: "f", 4: 'b'}
print(OrderedDict(sorted(test.items(), key=lambda obj: obj[0])))
print(OrderedDict(