• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python模块简介之有序字典(OrderedDict)

python模块简介之有序字典(OrderedDict)

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python ordereddict,ordereddict,ordereddict 强转,python模块大全,python模块等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

有序字典-OrderedDict简介

示例

有序字典和通常字典类似,只是它可以记录元素插入其中的顺序,而一般字典是会以任意的顺序迭代的。参见下面的例子:

import collections

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

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

print '\nOrderedDict:'
d = collections.OrderedDict()
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'

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

</div>

运行结果如下:

-> python test7.py
Regular dictionary:
a A
c C
b B
e E
d D

OrderedDict:
a A
b B
c C
d D
e E

</div>

可以看到通常字典不是以插入顺序遍历的。

相等性

判断两个有序字段是否相等(==)需要考虑元素插入的顺序是否相等

import collections

print 'dict    :',
d1 = {}
d1['a'] = 'A'
d1['b'] = 'B'
d1['c'] = 'C'
d1['d'] = 'D'
d1['e'] = 'E'

d2 = {}
d2['e'] = 'E'
d2['d'] = 'D'
d2['c'] = 'C'
d2['b'] = 'B'
d2['a'] = 'A'

print d1 == d2

print 'OrderedDict:',

d1 = collections.OrderedDict()
d1['a'] = 'A'
d1['b'] = 'B'
d1['c'] = 'C'
d1['d'] = 'D'
d1['e'] = 'E'

d2 = collections.OrderedDict()
d2['e'] = 'E'
d2['d'] = 'D'
d2['c'] = 'C'
d2['b'] = 'B'
d2['a'] = 'A'

print d1 == d2

</div>

运行结果如下:

-> python test7.py
dict    : True
OrderedDict: False
</div>

而当判断一个有序字典和其它普通字典是否相等只需判断内容是否相等。

注意

OrderedDict 的构造器或者 update() 方法虽然接受关键字参数,但因为python的函数调用会使用无序的字典来传递参数,所以关键字参数的顺序会丢失,所以创造出来的有序字典不能保证其顺序。

参考资料

https://docs.python.org/2/library/collections.html#collections.OrderedDict
https://pymotw.com/2/collections/ordereddict.html

</div>

您可能想查找下面的文章:

  • python中OrderedDict的使用方法详解
  • python中OrderedDict的使用方法详解
  • python模块简介之有序字典(OrderedDict)
  • Python的collections模块中的OrderedDict有序字典
  • Python的collections模块中的OrderedDict有序字典

相关文章

  • 详解Python中列表和元祖的使用方法
  • Python标准库之多进程(multiprocessing包)介绍
  • 使用PDB简单调试Python程序简明指南
  • Python enumerate遍历数组示例应用
  • python获取外网ip地址的方法总结
  • python操作 hbase 数据的方法
  • 简单介绍Python中的JSON模块
  • python字符串中的单双引
  • Python中shutil模块的学习笔记教程
  • 简单谈谈Python中的反转字符串问题

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python中的__init__ 、__new__、__call__小结
    • python通过ftplib登录到ftp服务器的方法
    • python中的字典详细介绍
    • 详解python基础之while循环及if判断
    • 浅谈Python中的闭包
    • Python 常用string函数详解
    • 从零学python系列之数据处理编程实例(一)
    • Python 通过pip安装Django详细介绍
    • python操作 hbase 数据的方法
    • Python写的Discuz7.2版faq.php注入漏洞工具

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有