Python有序字典简单实现方法示例

时间:2022-10-21 12:17:30

本文实例讲述了Python有序字典简单实现方法。分享给大家供大家参考,具体如下:

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- 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

执行结果:

Python有序字典简单实现方法示例

 

希望本文所述对大家Python程序设计有所帮助。

原文链接:http://blog.csdn.net/xiecj_2006/article/details/42320279