Python中使用bidict模块双向字典结构的奇技淫巧
2016-07-12 00:00
721 查看
快速入门
模块提供三个类来处理一对一映射类型的一些操作
'bidict', 'inverted', 'namedbidict'
1.bidict类:
2.inverted类,反转字典的键值
3.namedbidict(mapname, fwdname, invname):
更多内容
如果你不喜欢冒号的方式,可以使用namedbidict类给双向字典起2个别名。这样对外会提供正向和逆向的2个子字典。实际上还是以一个双向 字典的形式存在:
还可以使用一元的逆运算符"~"获取bidict逆映射字典。
以下情况注意添加括号,因为~的优先级低于中括号:
以下情况注意添加括号,因为~的优先级低于中括号:
bidict不是dict的子类,但它的API的是dict的超集(但没有fromkeys方法,改用了MutableMapping接 口)。
迭代器类inverted会翻转key和value,如:
bidict的invert()方法和inverted类似。依赖模块:collections中的MutableMapping,functools中的wraps,re。
bidict可以和字典进行比较
其他字典通用的方法,bidict也支持:
Python的collections模块中的OrderedDict有序字典
Python处理json字符串转化为字典的简单实现
全面了解python字符串和字典
python 字典(dict)按键和值排序
Python构造自定义方法来美化字典结构输出的示例
举例讲解Python中字典的合并值相加与异或对比
详解字典树Trie结构及其Python代码实现
python操作字典类型的常用方法(推荐)
Python中遍历字典过程中更改元素导致异常的解决方法
Python数据类型详解(四)字典:dict
Python实现简单字典树的方法
Python的“二维”字典 (two-dimension dictionary)定义与实现方法
模块提供三个类来处理一对一映射类型的一些操作
'bidict', 'inverted', 'namedbidict'
>>> import bidict >>> dir(bidict) ['MutableMapping', '_LEGALNAMEPAT', '_LEGALNAMERE', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'bidict', 'inverted', 'namedbidict', 're', 'wraps']
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'})
2.inverted类,反转字典的键值
>>> seq = [(1, 'one'), (2, 'two'), (3, 'three')] >>> list(inverted(seq)) [('one', 1), ('two', 2), ('three', 3)]
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'})
更多内容
如果你不喜欢冒号的方式,可以使用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'
还可以使用一元的逆运算符"~"获取bidict逆映射字典。
>>> import bidict >>> from bidict import bidict >>> husbands2wives = bidict({'john': 'jackie'}) >>> ~husbands2wives bidict({'jackie': 'john'})
以下情况注意添加括号,因为~的优先级低于中括号:
>>> import bidict >>> from bidict import bidict >>> husbands2wives = bidict({'john': 'jackie'}) >>> ~husbands2wives bidict({'jackie': 'john'})
以下情况注意添加括号,因为~的优先级低于中括号:
>>> (~bi)['one'] 1
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)]
bidict的invert()方法和inverted类似。依赖模块:collections中的MutableMapping,functools中的wraps,re。
bidict可以和字典进行比较
>>> bi == bidict({1:'one'}) >>> bi == dict([(1, 'one')]) True
其他字典通用的方法,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)]
您可能感兴趣的文章:
Python常用的内置序列结构(列表、元组、字典)学习笔记Python的collections模块中的OrderedDict有序字典
Python处理json字符串转化为字典的简单实现
全面了解python字符串和字典
python 字典(dict)按键和值排序
Python构造自定义方法来美化字典结构输出的示例
举例讲解Python中字典的合并值相加与异或对比
详解字典树Trie结构及其Python代码实现
python操作字典类型的常用方法(推荐)
Python中遍历字典过程中更改元素导致异常的解决方法
Python数据类型详解(四)字典:dict
Python实现简单字典树的方法
Python的“二维”字典 (two-dimension dictionary)定义与实现方法
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法