使用 Bidict 类实现双向哈希表
双向哈希表提供了按同一数据中的键和值进行索引的能力结构。 Python 的原生字典对于单向映射来说是一种很有价值的数据结构,但在双向查找方面却存在不足。本文介绍了一种在 Python 中实现双向哈希表的有效方法。
实现细节
实现的核心是 bidict 类,它扩展了 Python 的标准字典。该类维护两个字典:一个用于标准键值映射,另一个用于值键映射的逆字典。
主要功能
bidict 类提供几个值得注意的功能:
- 自动更新逆向目录:当标准字典被修改(通过项目添加、修改或删除)时,逆向字典会自动更新。
- 相同值的键列表:与其他一些双向 dict 实现不同,bidict 允许多个键具有相同的值。
- 高效查找:利用原生 Python 字典实现,在恒定时间内执行键或值的检索。
使用示例
为了演示其功能,让我们创建一个 bidict并操作它:
<code class="python">import numpy as np bd = bidict(zip(['a', 'b'], np.random.randint(2, size=2))) print(bd) # {'a': 1, 'b': 0} print(bd.inverse) # {1: ['a'], 0: ['b']}</code>
我们可以修改键“a”的值:
<code class="python">bd['a'] = 0 print(bd) # {'b': 0, 'a': 0} print(bd.inverse) # {0: ['b', 'a']}</code>
请注意,逆向字典会自动更新以反映更改。我们还可以从字典中删除项目:
<code class="python">del bd['a'] print(bd) # {'b': 0} print(bd.inverse) # {0: ['b']}</code>
同样,逆字典无缝地调整到删除。
总之,bidict 类提供了一种高效且方便的双向实现Python 中的哈希表,提供自动更新逆向目录、支持多个具有相同值的键和恒定时间查找。
以上是如何在 Python 中实现双向哈希表?的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能