当前位置:Gxlcms > Python > Python中使用bidict模块双向字典结构的奇技淫巧

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

时间:2021-07-01 10:21:17 帮助过:75人阅读

快速入门

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

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

1.bidict类:

  1. >>> from bidict import bidict
  2. >>> D=bidict({'a':'b'})
  3. >>> D['a']
  4. 'b'
  5. >>> D[:'b']
  6. 'a'
  7. >>> ~D #反转字典
  8. bidict({'b': 'a'})
  9. >>> dict(D) #转为普通字典
  10. {'a': 'b'}
  11. >>> D['c']='c' #添加元素,普通字典的方法都可以用
  12. >>> D
  13. bidict({'a': 'b', 'c': 'c'})

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

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

3.namedbidict(mapname, fwdname, invname):

  1. >>> CoupleMap = namedbidict('CoupleMap', 'husbands', 'wives')
  2. >>> famous = CoupleMap({'bill': 'hillary'})
  3. >>> famous.husbands['bill']
  4. 'hillary'
  5. >>> famous.wives['hillary']
  6. 'bill'
  7. >>> famous.husbands['barack'] = 'michelle'
  8. >>> del famous.wives['hillary']
  9. >>> famous
  10. CoupleMap({'barack': 'michelle'})

更多内容

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

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

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

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

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

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

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

  1. >>> (~bi)['one']
  2. 1

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

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

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

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

bidict可以和字典进行比较

  1. >>> bi == bidict({1:'one'})
  2. >>> bi == dict([(1, 'one')])
  3. True

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

  1. >>> bi.get('one')
  2. 1
  3. >>> bi.setdefault('one', 2)
  4. 1
  5. >>> bi.setdefault('two', 2)
  6. 2
  7. >>> len(bi) # calls __len__
  8. 2
  9. >>> bi.pop('one')
  10. 1
  11. >>> bi.popitem()
  12. ('two', 2)
  13. >>> bi.inv.setdefault(3, 'three')
  14. 'three'
  15. >>> bi
  16. bidict({'three': 3})
  17. >>> [key for key in bi] # calls __iter__, returns keys like dict
  18. ['three']
  19. >>> 'three' in bi # calls __contains__
  20. True
  21. >>> list(bi.keys())
  22. ['three']
  23. >>> list(bi.values())
  24. [3]
  25. >>> bi.update([('four', 4)])
  26. >>> bi.update({'five': 5}, six=6, seven=7)
  27. >>> sorted(bi.items(), key=lambda x: x[1])
  28. [('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7)]

人气教程排行