脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python有序字典简单实现方法示例

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

2020-12-10 00:24xiecj_2006 Python

这篇文章主要介绍了Python有序字典简单实现方法,涉及Python使用OrderedDict方法进行字典排序的相关操作技巧,需要的朋友可以参考下

本文实例讲述了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

延伸 · 阅读

精彩推荐