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

Python中使用bidict模块双向字典结构的奇技淫巧

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

通过本文主要向大家介绍了python 字典,python 字典排序,python字典添加元素,python 遍历字典,python字典操作等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

快速入门

模块提供三个类来处理一对一映射类型的一些操作
'bidict', 'inverted', 'namedbidict'

>>> import bidict
>>> dir(bidict)
['MutableMapping', '_LEGALNAMEPAT', '_LEGALNAMERE', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'bidict', 'inverted', 'namedbidict', 're', 'wraps']
 
</div>

1.bidict类: 

>>> from bidict import bidict
>>> D=bidict({'a':'b'})
>>> D['a']
'b'
>>> D[:'b']
'a'
>>> ~D        #反转字典
bidict({'b': 'a'})
>>> dict(D)    #转为普通字典
{'a': 'b'}
>>> D['c']='c'   #添加元素,普通字典的方法都可以用
>>> D
bidict({'a': 'b', 'c': 'c'}) 
</div>

2.inverted类,反转字典的键值

>>> seq = [(1, 'one'), (2, 'two'), (3, 'three')]
>>> list(inverted(seq))
    [('one', 1), ('two', 2), ('three', 3)]
</div>

3.namedbidict(mapname, fwdname, invname):

>>> CoupleMap = namedbidict('CoupleMap', 'husbands', 'wives')
>>> famous = CoupleMap({'bill': 'hillary'})
>>> famous.husbands['bill']
'hillary'
>>> famous.wives['hillary']
'bill'
>>> famous.husbands['barack'] = 'michelle'
>>> del famous.wives['hillary']
>>> famous
CoupleMap({'barack': 'michelle'})
</div>

更多内容

如果你不喜欢冒号的方式,可以使用namedbidict类给双向字典起2个别名。这样对外会提供正向和逆向的2个子字典。实际上还是以一个双向 字典的形式存在:

>>> HTMLEntities = namedbidict('HTMLEntities', 'names', 'codepoints')
>>> entities = HTMLEntities({'lt': 60, 'gt': 62, 'amp': 38}) # etc
>>> entities.names['lt']
60
>>> entities.codepoints[38]
'amp'
</div>

还可以使用一元的逆运算符"~"获取bidict逆映射字典。

>>> import bidict
>>> from bidict import bidict
>>> husbands2wives = bidict({'john': 'jackie'})
>>> ~husbands2wives
bidict({'jackie': 'john'})
</div>

以下情况注意添加括号,因为~的优先级低于中括号:

>>> import bidict
>>> from bidict import bidict
>>> husbands2wives = bidict({'john': 'jackie'})
>>> ~husbands2wives
bidict({'jackie': 'john'})
</div>

以下情况注意添加括号,因为~的优先级低于中括号:

>>> (~bi)['one']
1
</div>

bidict不是dict的子类,但它的API的是dict的超集(但没有fromkeys方法,改用了MutableMapping接 口)。

迭代器类inverted会翻转key和value,如:

>>> seq = [(1, 'one'), (2, 'two'), (3, 'three')]
>>> list(inverted(seq))
[('one', 1), ('two', 2), ('three', 3)]
</div>

bidict的invert()方法和inverted类似。依赖模块:collections中的MutableMapping,functools中的wraps,re。

bidict可以和字典进行比较

>>> bi == bidict({1:'one'})
>>> bi == dict([(1, 'one')])
True
</div>

其他字典通用的方法,bidict也支持:

>>> bi.get('one')
1
>>> bi.setdefault('one', 2)
1
>>> bi.setdefault('two', 2)
2
>>> len(bi) # calls __len__
2
>>> bi.pop('one')
1
>>> bi.popitem()
('two', 2)
>>> bi.inv.setdefault(3, 'three')
'three'
>>> bi
bidict({'three': 3})
>>> [key for key in bi] # calls __iter__, returns keys like dict
['three']
>>> 'three' in bi # calls __contains__
True
>>> list(bi.keys())
['three']
>>> list(bi.values())
[3]
>>> bi.update([('four', 4)])
>>> bi.update({'five': 5}, six=6, seven=7)
>>> sorted(bi.items(), key=lambda x: x[1])
[('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7)]
</div>

</div>

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

  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • 详解Python中类的定义与使用
  • python获取指定时间差的时间实例详解
  • 详解Python中类的定义与使用
  • python获取指定时间差的时间实例详解
  • Python 类的继承实例详解
  • python 类详解及简单实例

相关文章

  • python目录与文件名操作例子
  • python简单实现基数排序算法
  • 在arcgis使用python脚本进行字段计算时是如何解决中文问题的
  • Python深入学习之闭包
  • 详解Python中的元组与逻辑运算符
  • 浅谈Python的文件类型
  • Python中的super()方法使用简介
  • 在Linux系统上通过uWSGI配置Nginx+Python环境的教程
  • Python中的高级数据结构详解
  • python re正则表达式模块(Regular Expression)

文章分类

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

最近更新的内容

    • python动态参数用法实例分析
    • Python yield 小结和实例
    • python 迭代器和iter()函数详解及实例
    • Python错误提示:[Errno 24] Too many open files的分析与解决
    • Python内置函数的用法实例教程
    • Python中str.format()详解
    • django实现分页的方法
    • 以windows service方式运行Python程序的方法
    • Python的SQLalchemy模块连接与操作MySQL的基础示例
    • Python内置数据结构与操作符的练习题集锦

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

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