python 中实现字典的项按照字典的键进行排序
001、
>>> test1 = dict(c = 200, a = 400, d = 300, b = 700) >>> test1 {'c': 200, 'a': 400, 'd': 300, 'b': 700} >>> test2 = {} >>> temp = [] >>> for i in test1: ... temp.append(i) ... >>> temp ['c', 'a', 'd', 'b'] >>> temp.sort() ## 对字典的键进行排序 >>> temp ['a', 'b', 'c', 'd'] >>> for i in temp: ... test2[i] = test1[i] ## 按照排序后的键输出字典 ... >>> test2 {'a': 400, 'b': 700, 'c': 200, 'd': 300}

浙公网安备 33010602011771号