目录

Python如何让字典保持有序存储

前景

众所周知python的字典dict是无序的和元组不同,但是一些特定场景,又需要字典中的数据是有序的,分享并记录下如何解决

注意

python3.7之后字典就更改为有序存储了,不需要重新定义,python3.7之前的可以用以下方法重新定义为有序存储

案例

无序演示

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
dict1 = {'a':1,"b":2,"c":3}

print(dict1)

#多次执行,结果如下:
{'a':1,"b":2,"c":3}

{'a':1,"b":2,"c":3}

{"b":2,'a':1,"c":3}

{"c":3,'a':1,"b":2}

{'a':1,"c":3,"b":2}

解决

有序演示

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import collections
dict2 = collections.OrderedDict()
dict2['a'] = 1 
dict2['b'] = 2
dict2['c'] = 3

print(dict2)

#多次执行,结果如下:
{'a':1,"b":2,"c":3}

{'a':1,"b":2,"c":3}

{'a':1,"b":2,"c":3}

{'a':1,"b":2,"c":3}

{'a':1,"b":2,"c":3}