Software Development

Bidirectional Hash desk or Two method dictionary in Python

Bidirectional Hash desk or Two method dictionary in Python
Written by admin


We learn about Python dictionaries in an information construction in Python which holds knowledge within the type of key: worth pairs. On this article, we’ll talk about the Bidirectional Hash desk or Two-way dictionary in Python. We will say a two-way dictionary could be represented as key ⇐⇒ worth. One instance of two-way dictionaries is:

Instance:

dict={ 1 : 'Apple' , 2 : 'Google' , 3 : 'Microsoft'}

Enter 1: 1
Output 1: Apple

Enter 2: Microsoft
Output 2: 3

Explaination:The above dictionary maps the keys ( integers) to the values (firm names) on this case.

A bidirectional dictionary could be represented as key ⇐⇒ worth. I.e. it may possibly return worth based mostly on the important thing and likewise the corresponding key on the premise of the worth. Within the above instance, a daily dictionary could be regarded up utilizing 1,2,3 which might return Apple, Google, and Microsoft respectively. Nonetheless in a bidirectional dictionary, we are able to lookup the dictionary utilizing 1,2, and three in addition to Apple, Google, and Microsoft which might return 1,2,3 respectively.

Stepwise Implementation

Step 1: Putting in the bidict library.

This library permits us to work with bidirectional hash tables or two-way dictionaries. To put in the bidict library we have to use the next command:

pip set up bidict

Step 2: Importing the bidict class from the bidict module

Python

from bidict import bidict

Step 3: Create a daily dictionary.

Making a dictionary in python is easy. We will probably be making a dictionary known as dict_it_fullforms which maps the generally used IT quick types to their full types.

Python3

dict_it_fullforms={'APK':'Android Software Bundle',

                   'CPU':'Central Processing Unit',

                   'SMS':'Quick Message Service',

                   'USB':'Common Serial Bus',

                   'WIFI':'Wi-fi Constancy',

                   'WWW':'World Large Internet'}

Step 4: Making a bidict object

Create a 2-way dictionary by making a bidict object bidict_it_fullforms utilizing dict_it_fullforms.

Python3

bidict_it_fullforms = bidict(dict_it_fullforms)

Step 5: Lookup utilizing quick types

Right here we use the keys to print the values of bidict_it_fullforms.

Python3

print(bidict_it_fullforms['APK'])

print(bidict_it_fullforms['SMS'])

print(bidict_it_fullforms['WIFI'])

Output:

Android Software Bundle
Quick Message Service
Wi-fi Constancy

Step 6: An Inverse attribute of bidict object

To be able to get the keys of respective full types we have to use an inverse attribute of the bidict_it_fullforms object.

Python3

bidict_it_shortforms = bidict_it_fullforms.inverse

Step 7: Lookup utilizing full types

We now have bidict_it_shortforms as a bidict object reference that can be utilized to retrieve keys utilizing values. Therefore we are able to get the quick types utilizing the total types.

Python3

print(bidict_it_shortforms['Central Processing Unit'])

print(bidict_it_shortforms['Universal Serial Bus'])

print(bidict_it_shortforms['World Wide Web'])

Output:

CPU
USB
WWW

Step 8: Modifications or Additions

If any adjustments or key-value additions are made to bidict_it_shortforms it can replicate in bidict_it_fullforms and vice versa. Allow us to add the total type of SIM. 

Python3

bidict_it_shortforms['Subscriber Identity Module']='SIM'

print(bidict_it_fullforms['SIM'])

Output:

Subscriber Identification Module

Full Code:

Python3

  

from bidict import bidict

  

dict_it_fullforms = {'APK': 'Android Software Bundle'

                     'CPU': 'Central Processing Unit',

                     'SMS': 'Quick Message Service'

                     'USB': 'Common Serial Bus'

                     'WIFI': 'Wi-fi Constancy'

                     'WWW': 'World Large Internet'}

  

bidict_it_fullforms = bidict(dict_it_fullforms)

  

print(bidict_it_fullforms['APK'])

print(bidict_it_fullforms['SMS'])

print(bidict_it_fullforms['WIFI'])

  

bidict_it_shortforms = bidict_it_fullforms.inverse

  

print(bidict_it_shortforms['Central Processing Unit'])

print(bidict_it_shortforms['Universal Serial Bus'])

print(bidict_it_shortforms['World Wide Web'])

  

bidict_it_shortforms['Subscriber Identity Module'] = 'SIM'

print(bidict_it_fullforms['SIM'])

Output:

Android Software Bundle
Quick Message Service
Wi-fi Constancy
CPU
USB
WWW
Subscriber Identification Module

About the author

admin

Leave a Comment