Sunday, February 5, 2023
HomeSoftware DevelopmentBidirectional Hash desk or Two approach dictionary in Python

Bidirectional Hash desk or Two approach dictionary in Python


We learn about Python dictionaries in a knowledge construction in Python which holds knowledge within the type of key: worth pairs. On this article, we are going to talk about the Bidirectional Hash desk or Two-way dictionary in Python. We are able to 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 well return worth primarily based on the important thing and in addition the corresponding key on the idea of the worth. Within the above instance, a daily dictionary could be appeared up utilizing 1,2,3 which might return Apple, Google, and Microsoft respectively. Nonetheless in a bidirectional dictionary, we are able to search for 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 straightforward. We might be making a dictionary referred to as dict_it_fullforms which maps the generally used IT brief types to their full types.

Python3

dict_it_fullforms={'APK':'Android Utility Package deal',

                   'CPU':'Central Processing Unit',

                   'SMS':'Quick Message Service',

                   'USB':'Common Serial Bus',

                   'WIFI':'Wi-fi Constancy',

                   'WWW':'World Vast Net'}

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 brief 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 Utility Package deal
Quick Message Service
Wi-fi Constancy

Step 6: An Inverse attribute of bidict object

With a purpose 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 brief types utilizing the complete 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: Adjustments or Additions

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

Python3

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

print(bidict_it_fullforms['SIM'])

Output:

Subscriber Id Module

Full Code:

Python3

  

from bidict import bidict

  

dict_it_fullforms = {'APK': 'Android Utility Package deal'

                     'CPU': 'Central Processing Unit',

                     'SMS': 'Quick Message Service'

                     'USB': 'Common Serial Bus'

                     'WIFI': 'Wi-fi Constancy'

                     'WWW': 'World Vast Net'}

  

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 Utility Package deal
Quick Message Service
Wi-fi Constancy
CPU
USB
WWW
Subscriber Id Module

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments