python2和python3.6之前的python版本,默认的字典类型都是无序的,若需要用有序的字典,可以用collection包中的OrderedDict来实现
from collections import OrderedDict
print("Regular dictionary")
d={}
d['a']='A'
d['b']='B'
d['c']='C'
print(d)
print("Order dictionary")
d1 = OrderedDict()
d1['a'] = 'A'
d1['b'] = 'B'
d1['c'] = 'C'
print(d1)
在python2的编译器环境(python2的在线环境)下,输出为
Regular dictionary
{'a': 'A', 'c': 'C', 'b': 'B'}
Order dictionary
OrderedDict([('a', 'A'), ('b', 'B'), ('c', 'C')])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)