Python - 字典中的字首鍵匹配


介紹

Python 是一種靈活的程式語言,以其簡單性和可讀性而聞名。其強大的功能之一是在字典中執行字首鍵匹配的能力。此功能允許有效地搜尋以特定字首開頭的鍵。在本文中,我們將探討在 Python 中實現字首鍵匹配的三種方法,以及它們的相應演算法、分步說明、Python 語法和程式碼示例。透過利用這些方法,我們可以顯著提高有效地處理和提取資料的能力。讓我們深入瞭解字首鍵匹配的世界吧!

方法一:線性搜尋

演算法

線性搜尋方法是在字典中執行字首鍵匹配的直接方法。它涉及遍歷所有鍵並檢查每個鍵是否以所需的鍵字首開頭。以下是此方法的演算法描述和分步說明:

  • 步驟 1 - 定義一個名為 `prefix_match_linear()` 的函式,並初始化一個空列表來儲存匹配的鍵。

  • 步驟 2 - 迭代字典中的每個鍵。

  • 步驟 3 - 檢查鍵是否以指定的字首開頭。

  • 步驟 4 - 如果找到字首匹配,則將鍵新增到列表中。

  • 步驟 5 - 對所有鍵重複步驟 3-4。

  • 步驟 6 - 返回匹配鍵的列表。

示例

def prefix_match_linear(dictionary, prefix):
    matches = []
    for key in dictionary.keys():
        if key.startswith(prefix):
            matches.append(key)
    return matches


fruit_dict = {
    'apple': 1,
    'apricot': 2,
    'banana': 3,
    'orange': 4,
    'pineapple': 5
}

prefix = 'app'
matches = prefix_match_linear(fruit_dict, prefix)
print(matches)

輸出

['apple']

方法二:Trie 資料結構

Trie 資料結構是一種樹狀結構,非常擅長高效的字首匹配。讓我們研究如何使用 Trie 資料結構來執行字首鍵匹配:

演算法

  • 步驟 1 - 定義一個包含多個元素的字典。

  • 步驟 2 - 分別建立兩個名為 `TrieNode` 和 `Trie` 的類。在 `TrieNode` 中建立無引數建構函式,並使用某些值設定其屬性。

  • 步驟 3 - 類似地,在 `Trie` 類中定義建構函式,並在 `Trie` 類中建立使用者定義函式 `insert`,並使用 for 迴圈迭代字典中的每個鍵。

  • 步驟 4 - 將每個鍵插入到 Trie 中。

  • 步驟 5 - 在 Trie 中搜索指定的字首。

  • 步驟 6 - 檢索所有與字首匹配的鍵。

示例

fruit_dict = {
    'apple': 1,
    'apricot': 2,
    'banana': 3,
    'orange': 4,
    'pineapple': 5
}

class TrieNode:
    def __init__(self):
        self.children = {}
        self.is_end_of_word = False


class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                node.children[char] = TrieNode()
            node = node.children[char]
        node.is_end_of_word = True

    def get_matches(self, prefix):
        node = self.root
        matches = []
        for char in prefix:
            if char not in node.children:
                return matches
            node = node.children[char]
        self._collect_matches(node, prefix, matches)
        return matches

    def _collect_matches(self, node, prefix, matches):
        if node.is_end_of_word:
            matches.append(prefix)
        for char, child in node.children.items():
            self._collect_matches(child, prefix + char, matches)


def prefix_match_trie(dictionary, prefix):
    trie = Trie()
    for key in dictionary.keys():
        trie.insert(key)
    return trie.get_matches(prefix)


prefix = 'ban'
matches = prefix_match_trie(fruit_dict, prefix)
print(matches)

輸出

['banana']

方法三:使用 Python 的內建 filter 函式

Python 提供了一個內建的 `filter()` 函式,允許我們建立一個高效的單行程式來執行字首鍵匹配。透過將此函式應用於字典鍵和 lambda 函式,我們可以實現簡潔明瞭的程式碼。以下是它的工作原理:

演算法

  • 步驟 1 - 建立一個名為 `fruit_dict` 的字典。

  • 步驟 2 - 定義一個名為 `prefix_match_filter()` 的函式,該函式在函式定義中包含兩個引數,然後建立一個 lambda 函式來檢查每個鍵是否以所需的字首開頭。

  • 步驟 3 - 使用 lambda 函式作為過濾器條件,將 `filter()` 函式應用於字典鍵。

  • 步驟 4 - 將結果鍵收集到一個列表中。

  • 步驟 5 - 呼叫函式並將它的值傳遞給名為 `matches` 的變數。

  • 步驟 6 - 最後,列印 `matches` 的值。

示例

fruit_dict = {
    'apple': 1,
    'apricot': 2,
    'banana': 3,
    'orange': 4,
    'pineapple': 5
}

def prefix_match_filter(dictionary, prefix):
    matches = list(filter(lambda key: key.startswith(prefix), dictionary.keys()))
    return matches

prefix = 'or'
matches = prefix_match_filter(fruit_dict, prefix)
print(matches)

輸出

['orange']

結論

在本文中,我們研究了在 Python 字典中執行字首鍵匹配的三種方法。我們介紹了線性搜尋方法,它很簡單,但對於大型資料集效率較低。然後,我們深入研究了 Trie 資料結構,它在效率方面優於字首匹配。每種方法都有其自身的優點,可以選擇哪種方法取決於手頭任務的要求。透過掌握 Python 中的字首鍵匹配技術,開發人員可以有效地從字典中檢索資料並簡化他們的資料處理任務。

更新於:2023年9月1日

瀏覽量:276

開啟你的職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.