Quick Start
Module provides three classes to handle some of the actions of a one-to-one mapping type
' Bidict ', ' inverted ', ' namedbidict '
>>> import bidict
>>> dir (bidict)
[' mutablemapping ', ' _legalnamepat ', ' _legalnamere ', ' __ Builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' bidict ', ' inverted ', ' namedbidict ', ' re ', ' wraps '
1.bidict class:
>>> 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 ' #添加元素, Common dictionary methods can be used
>>> D
bidict ({' A ': ' B ', ' C ': ' C '})
2.inverted class, reversing the key value of the dictionary
>>> 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['] = ' Michelle '
>>> del famous.wives[' Hillary ']
>>> Famous
couplemap ({' Barack ': ' Michelle '})
More content
If you don't like the colon, you can use the Namedbidict class to give the two-way dictionary 2 aliases. This will provide a forward and reverse 2 sub dictionaries. It actually exists in the form of a two-way dictionary:
>>> htmlentities = namedbidict (' htmlentities ', ' names ', ' codepoints ')
>>> entities = Htmlentities ({' Lt ': M, ' GT ': +, ' amp ':} ') # etc
>>> entities.names[' lt ']
>>> ENTITIES.CODEPOINTS[38]
' amp '
You can also use the unary inverse operator "~" to get the bidict inverse mapping dictionary.
>>> Import bidict
>>> from bidict import bidict
>>> husbands2wives = bidict ({' John ': ' Jackie '}
>>> ~husbands2wives
bidict ({' Jackie ': ' John '})
Note that the parentheses are added because the ~ has a lower precedence than the brackets :
>>> Import bidict
>>> from bidict import bidict
>>> husbands2wives = bidict ({' John ': ' Jackie '})
>>> ~husbands2wives
bidict ({' Jackie ': ' John '})
Note that the parentheses are added because the ~ has a lower precedence than the brackets:
Bidict is not a subclass of Dict, but its API is a dict superset (but there is no Fromkeys method, instead of the Mutablemapping interface).
The iterator class inverted flips key and value, such as:
>>> seq = [(1, ' One '), (2, ' two '), (3, ' three ')]
>>> list (inverted (seq))
[(' One ', 1), (' Two ', 2) , (' Three ', 3)]
The invert () method of bidict is similar to inverted. Dependency module: The Wraps,re in the Mutablemapping,functools in collections.
Bidict can be compared with dictionaries
>>> bi = = bidict ({1: ' one '})
>>> bi = dict ([(1, ' one ')]
True
Other common methods of dictionaries, Bidict also support:
>>> 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 into 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), (' fi ve ', 5), (' Six ', 6), (' Seven ', 7)]