Python – 查詢字典中具有特定字尾的鍵


鍵的字尾是在單詞末尾提到的一組字母。在這個問題陳述中,我們需要建立特定的變數,將字尾鍵設定為使用某些特定條件過濾結果。在 Python 中,我們有一些內建函式,例如 endswith()、filter()、lambda、append() 和 items(),將用於查詢字典中具有特定字尾的鍵。

讓我們來看一個例子。

給定的字典:

my_dict = {'compiler': 100, 'interpreter': 200, 'cooperative': 300, 'cloudbox': 400, 'database': 500}

字尾鍵設定為“er”,

因此,最終輸出變為 {'compiler': 100, 'interpreter': 20}

語法

以下語法在示例中使用

endswith()

這是 Python 中預定義的方法,如果字串以給定值結尾,則返回 true,否則返回 false。

filter()

當我們需要根據特定條件過濾專案時,將應用 filter() 方法。簡單來說,它允許迭代那些被提取以滿足條件的元素。

lambda

lambda 函式提供了一種使用 lambda 關鍵字宣告簡短匿名函式的快捷方式。lambda 函式只有一個表示式。

append()

append() 是 Python 中一個內建函式,它將元素插入列表的末尾。

items()

這是一個內建方法,可用於返回檢視物件。該物件包含鍵值對。

使用列表推導式

在下面的示例中,程式使用列表推導式技術,其中變數 key 迭代輸入字典,並使用 if 語句設定具有特定字尾的鍵的條件,藉助內建函式 endwith() 顯示結果。

示例

def key_suffix(dict, suffix):
    return [key for key in dict if key.endswith(suffix)]
# Create the dictionary
my_dict = {"dropbox": 1, "box": 2, "chat": 3, "mail": 4}
sfx = "ox"
# Calling function
res = key_suffix(my_dict, sfx)
# Display the result
print("Keys with suffix","[", sfx, "]", ":\n", res)

輸出

Keys with suffix [ ox ] : 
['dropbox', 'box'] 

使用 filter() 函式

在下面的示例中,程式使用名為 key_suffix 的遞迴函式,該函式接受兩個引數 - dict 和 suffix 來接收輸入字典和字尾值。然後使用一些內建函式 - filter()[過濾給定字尾元素以獲得結果]、lambda[使用內建函式 endswith() 計算字尾操作],最後它將使用內建函式 list() 將字尾值作為列表的形式獲取結果。

示例

def key_suffix(dict, suffix):
    return list(filter(lambda key: key.endswith(suffix), dict))
# Create the list
my_dict = {"Evergreen": 1, "Beautiful": 2, "Fruitful": 3, "Golden": 4, "Brass": 5}
sfx = "ful"
# Calling function
res = key_suffix(my_dict, sfx)
# Display the display
print("Keys with suffix","[", sfx, "]", ":\n", res)

輸出

 Keys with suffix [ ful ] :
 ['Beautiful', 'Fruitful']

使用迴圈和條件語句

在下面的示例中,程式使用遞迴函式設定空列表,該列表在函式返回期間以列表的形式儲存結果。然後使用 for 迴圈迭代程式的輸入字典,並使用 if 語句使用內建函式 endswith() 和 append() 設定具有特定字尾的鍵的條件。接下來,它將實現函式返回以獲取特定字尾作為結果。

示例

def key_suffix(dict, suffix):
    keys_with_suffix = []
# Set the condition for operation and iteration of the specific suffix
    for key in dict:
        if key.endswith(suffix):
            keys_with_suffix.append(key)
    return keys_with_suffix
# Create the dictionary
my_dict = {"apple": 1, "banana": 2, "cherry": 3, "orange": 4, "Grapes": 5}
sfx = "y"
# Calling function
res = key_suffix(my_dict, sfx)
print("Keys with suffix","[", sfx, "]", ":\n", res)

輸出

Keys with suffix [ y ] : 
['cherry'] 

字典推導式

在下面的示例中,我們將使用字典推導式,其中第一個引數設定為鍵值對。使用 items(),它將檢視物件檢查到字典中,並應用 if 語句使用內建函式 endswith() 設定鍵的條件,該函式指定具有特定字尾的鍵並生成結果。

示例

def key_suffix(dict, suffix):
    return {key: value for key, value in dict.items() if key.endswith(suffix)}

# Create the dictionary
my_dict = {"Anonyms": 1, "Synonyms": 2, "Metaphor": 3, "Irony": 4}
sfx = "ms"
# Calling function
res = key_suffix(my_dict, sfx)
print("Keys with suffix","[", sfx, "]", ":\n", res)

輸出

 Keys with suffix [ ms ] : 
{'Anonyms': 1, 'Synonyms': 2}

結論

我們知道字典中具有特定字尾的特定鍵是以字串形式表示的鍵,並以整數形式指定值。這裡使用了一些方法,例如字典推導式、列表推導式、filter() 和條件語句來解決這個問題陳述。這種型別的程式通常用於各種應用程式,例如資料分析、資料過濾、搜尋演算法上的邏輯構建等。

更新於:2023年8月16日

266 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.