Python uses the Bi- Al dictionary structure of the bidict Module

Source: Internet
Author: User

Python uses the Bi- Al dictionary structure of the bidict Module

Quick Start

The module provides three classes to handle one-to-one ing operations.
'Idict ', 'inverted', 'namedidict'

>>> 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 # reverse dictionary bidict ({'B': 'A'}) >>> dict (D) # convert to normal dictionary {'A ': 'B' }>>> D ['C'] = 'C' # to add elements. You can use the> Dbidict ({'A ': 'B', 'C': 'C '})

2. inverted class, which reverses the dictionary key value

>>> 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']>>> famousCoupleMap({'barack': 'michelle'})

More

If you do not like the colon method, you can use the namedbidict class to give two aliases to the bidirectional dictionary. In this way, two subdictionaries, forward and reverse, are provided. In fact, it still exists in the form of a bidirectional dictionary:

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

You can also use the unary inverse operator "~ "Obtain the bidict inverse ing dictionary.

>>> import bidict>>> from bidict import bidict>>> husbands2wives = bidict({'john': 'jackie'})>>> ~husbands2wivesbidict({'jackie': 'john'})

Note the following when adding brackets because ~ The priority is lower than the brackets:

>>> import bidict>>> from bidict import bidict>>> husbands2wives = bidict({'john': 'jackie'})>>> ~husbands2wivesbidict({'jackie': 'john'})

Note the following when adding brackets because ~ The priority is lower than the brackets:

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

Bidict is not a subclass of dict, but its API is the superset of dict (without the fromkeys method, instead of the MutableMapping interface ).

Inverted of the iterator class will flip the key and value, for example:

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

The invert () method of bidict is similar to that of inverted. Dependency module: MutableMapping in collections, wraps and re in functools.

Bidict can be compared with the dictionary

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

Other commonly used dictionary methods, bidict also supports:

>>> 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'>>> bibidict({'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)]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.