python有序字典

来源:互联网 发布:java实现301跳转 编辑:程序博客网 时间:2024/05/14 14:05

代码:

# -*- coding: UTF-8 -*-
import collections


print 'Regular dictionary:'
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'


for k, v in d.items():
    print k, v
    
print '\nOrderedDict:'
d = collections.OrderedDict()
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'


for k, v in d.items():
    print k, v


执行结果:

Regular dictionary:
a A
c C
b B


OrderedDict:
a A
b B
c C

0 0