Python中的双向哈希表(双向字典)

我们知道Python中的数据结构中的字典,它以键:值对的形式保存数据。在本文中,我们将讨论Python中的双向哈希表或双向字典。我们可以说一个双向字典可以表示为键的值。双向字典的一个例子是:

示例:

python 复制代码
dict={ 1 : 'Apple' , 2 : 'Google' , 3 : 'Microsoft'}

Input 1: 1
Output 1: Apple

Input 2: Microsoft
Output 2: 3

一个双向字典可以表示为关键字字典值。也就是说,它可以根据键返回值,也可以根据值返回相应的键。在上面的例子中,可以使用1,2,3来查找常规字典,这将分别返回Apple,Google和Microsoft。然而,在双向字典中,我们可以使用1,2和3以及Apple,Google和Microsoft分别返回1,2,3来查找字典。

实现过程

步骤1:安装bidict库

这个库使我们能够使用双向哈希表或双向字典。要安装bidict库,我们需要使用以下命令:

python 复制代码
pip install bidict

步骤2:从bidict模块中调用bidict类

python 复制代码
from bidict import bidict

步骤3:创建一个常规字典

在Python中创建字典很简单。我们将创建一个名为dict_it_fullforms的字典,它将常用的IT缩写形式映射到它们的完整形式。

python 复制代码
dict_it_fullforms={'APK':'Android Application Package',
				'CPU':'Central Processing Unit',
				'SMS':'Short Message Service',
				'USB':'Universal Serial Bus',
				'WIFI':'Wireless Fidelity',
				'WWW':'World Wide Web'}

步骤4:创建一个bidict对象

通过使用dict_it_fullforms创建bidict对象。

python 复制代码
bidict_it_fullforms = bidict(dict_it_fullforms)

步骤5:打印字典对象值

这里我们使用键来打印bidict_it_fullforms的值。

python 复制代码
print(bidict_it_fullforms['APK'])
print(bidict_it_fullforms['SMS'])
print(bidict_it_fullforms['WIFI'])

输出

python 复制代码
Android Application Package
Short Message Service
Wireless Fidelity

步骤6:bidict对象的Inverse属性

python 复制代码
bidict_it_shortforms = bidict_it_fullforms.inverse

步骤7:查看完整键值对

我们现在有了bidict_it_shortforms作为bidict对象引用,它可用于使用值检索键。因此,我们可以使用完整的形式来获得键值对。

python 复制代码
print(bidict_it_shortforms['Central Processing Unit'])
print(bidict_it_shortforms['Universal Serial Bus'])
print(bidict_it_shortforms['World Wide Web'])

输出

python 复制代码
CPU
USB
WWW

步骤8:修改或添加

如果对bidict_it_shortforms进行了任何更改或键值添加,它将反映在bidict_it_fullforms中,反之亦然。让我们添加SIM的完整形式。

python 复制代码
bidict_it_shortforms['Subscriber Identity Module']='SIM'
print(bidict_it_fullforms['SIM'])

输出

python 复制代码
Subscriber Identity Module

完整代码

python 复制代码
# Python implementation for bidirectional 
# hash table or two way dictionary.

# import the bidict class of the bidict module
from bidict import bidict

# creating a dictionary mapping commonly used 
# IT short forms to their full forms
dict_it_fullforms = {'APK': 'Android Application Package', 
					'CPU': 'Central Processing Unit',
					'SMS': 'Short Message Service', 
					'USB': 'Universal Serial Bus', 
					'WIFI': 'Wireless Fidelity', 
					'WWW': 'World Wide Web'}

# Creating a bidict object
bidict_it_fullforms = bidict(dict_it_fullforms)

# Lookup using short forms
print(bidict_it_fullforms['APK'])
print(bidict_it_fullforms['SMS'])
print(bidict_it_fullforms['WIFI'])

# Inverse attribute of bidict object
bidict_it_shortforms = bidict_it_fullforms.inverse

# Lookup using full forms
print(bidict_it_shortforms['Central Processing Unit'])
print(bidict_it_shortforms['Universal Serial Bus'])
print(bidict_it_shortforms['World Wide Web'])

# Adding SIM : Subscriber Identity Module to the bi-dictionary
bidict_it_shortforms['Subscriber Identity Module'] = 'SIM'
print(bidict_it_fullforms['SIM'])

输出

python 复制代码
Android Application Package
Short Message Service
Wireless Fidelity
CPU
USB
WWW
Subscriber Identity Module
相关推荐
篝火悟者12 分钟前
问题-python-运行报错-SyntaxError: Non-UTF-8 code starting with ‘\xd5‘ in file 汉字编码问题
开发语言·python
hakesashou32 分钟前
python如何比较字符串
linux·开发语言·python
_.Switch1 小时前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习
Hoper.J1 小时前
PyTorch 模型保存与加载的三种常用方式
人工智能·pytorch·python
弱冠少年2 小时前
websockets库使用(基于Python)
开发语言·python·numpy
技术无疆2 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
羊小猪~~3 小时前
机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
人工智能·python·机器学习·数据挖掘·数据分析·回归·时序数据库
qq_273900233 小时前
解析TMalign文本文件中的转换矩阵
python·生物信息学
阿华的代码王国4 小时前
【JavaEE】——文件IO的应用
开发语言·python
电饭叔4 小时前
《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
开发语言·python