我是靠谱客的博主 忐忑八宝粥,这篇文章主要介绍Python中使用bidict模块双向字典结构的奇技淫巧,现在分享给大家,希望可以做个参考。

快速入门

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

复制代码
1
2
3
>>> import bidict >>> dir(bidict) ['MutableMapping', '_LEGALNAMEPAT', '_LEGALNAMERE', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'bidict', 'inverted', 'namedbidict', 're', 'wraps']

1.bidict类: 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
>>> 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类,反转字典的键值

复制代码
1
2
3
>>> seq = [(1, 'one'), (2, 'two'), (3, 'three')] >>> list(inverted(seq)) [('one', 1), ('two', 2), ('three', 3)]

3.namedbidict(mapname, fwdname, invname):

复制代码
1
2
3
4
5
6
7
8
9
10
>>> 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个子字典。实际上还是以一个双向 字典的形式存在:

复制代码
1
2
3
4
5
6
>>> HTMLEntities = namedbidict('HTMLEntities', 'names', 'codepoints') >>> entities = HTMLEntities({'lt': 60, 'gt': 62, 'amp': 38}) # etc >>> entities.names['lt'] 60 >>> entities.codepoints[38] 'amp'

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

复制代码
1
2
3
4
5
>>> import bidict >>> from bidict import bidict >>> husbands2wives = bidict({'john': 'jackie'}) >>> ~husbands2wives bidict({'jackie': 'john'})

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

复制代码
1
2
3
4
5
>>> import bidict >>> from bidict import bidict >>> husbands2wives = bidict({'john': 'jackie'}) >>> ~husbands2wives bidict({'jackie': 'john'})

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

复制代码
1
2
>>> (~bi)['one'] 1

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

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

复制代码
1
2
3
>>> 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可以和字典进行比较

复制代码
1
2
3
>>> bi == bidict({1:'one'}) >>> bi == dict([(1, 'one')]) True

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
>>> 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中使用bidict模块双向字典结构的奇技淫巧的全部内容,更多相关Python中使用bidict模块双向字典结构内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(131)

评论列表共有 0 条评论

立即
投稿
返回
顶部