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
相关推荐
墨绿色的摆渡人7 分钟前
pytorch小记(二十二):全面解读 PyTorch 的 `torch.cumprod`——累积乘积详解与实战示例
人工智能·pytorch·python
小白—人工智能22 分钟前
数据分析 —— 数据预处理
python·数据挖掘·数据分析
若叶时代25 分钟前
数据分析_Python
人工智能·python·数据分析
英英_35 分钟前
python 爬虫框架介绍
开发语言·爬虫·python
大模型铲屎官2 小时前
【Python-Day 14】玩转Python字典(上篇):从零开始学习创建、访问与操作
开发语言·人工智能·pytorch·python·深度学习·大模型·字典
yunvwugua__2 小时前
Python训练营打卡 Day27
开发语言·python
Stara05113 小时前
基于多头自注意力机制(MHSA)增强的YOLOv11主干网络—面向高精度目标检测的结构创新与性能优化
人工智能·python·深度学习·神经网络·目标检测·计算机视觉·yolov11
那雨倾城4 小时前
使用 OpenCV 将图像中标记特定颜色区域
人工智能·python·opencv·计算机视觉·视觉检测
LuckyTHP6 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
mahuifa8 小时前
(7)python开发经验
python·qt·pyside6·开发经验