Python 中的雙向雜湊表實作
Python 的內建dict 是一個非常有用的資料結構,提供基於鍵的資料高效查找。然而,當需要從值進行反向查找時,就會發生這種情況。本文探討了實現雙向雜湊表的最有效方法,提供了一個受現有解決方案啟發並經過修改以滿足特定要求的範例。
實作
提出的解決方案使用自訂類別 bidict 來擴展標準 Python 的 dict。它維護一個額外的逆字典,每當 Bidict 被修改時,它就會自動更新。這個逆字典將值對應到分配有該值的鍵列表。
重要的是,與現有庫不同,此實作允許多個鍵具有相同的值。此功能在某些場景下至關重要。
用法
bidict 類別採用以下關鍵方法:
- bidict(args, kwargs ):建構函數,接受儲存在標準字典和逆字典中的參數和關鍵字參數。
- bidict.__setitem__(key, value):將鍵設為值。如果 key 已經在 bidict 中,它將從逆字典中與其目前值關聯的鍵列表中刪除 key。然後,它將鍵設為兩個字典中的值,更新與逆字典中的值關聯的鍵列表。
- bidict.__delitem__(key):從 bidict 中刪除鍵。它找到 key 的關聯值並相應地更新逆字典,從與 value 關聯的鍵列表中刪除 key。如果值的鍵列表變空,則從逆字典中刪除值。
範例用法
<code class="python">bd = bidict({'a': 1, 'b': 2}) print(bd.inverse) # {1: ['a'], 2: ['b']} bd['c'] = 1 print(bd.inverse) # {1: ['a', 'c'], 2: ['b']} del bd['c'] print(bd.inverse) # {1: ['a'], 2: ['b']}</code>
以上是如何在Python中實現具有相同值的多個鍵的雙向雜湊表?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增強效率和通用性。

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

在Python中,可以通過多種方法連接列表並管理重複元素:1)使用 運算符或extend()方法可以保留所有重複元素;2)轉換為集合再轉回列表可以去除所有重複元素,但會丟失原有順序;3)使用循環或列表推導式結合集合可以去除重複元素並保持原有順序。

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器