Python – 從字串列表中查詢字典鍵
字典是 Python 中著名的資料型別之一,它由鍵值對組成。字串列表是一組元素,這些元素用單引號或雙引號表示。在 Python 中,有一些內建函式,例如 keys()、set()、intersection 和 append(),可用於查詢字串列表中存在的字典鍵。
讓我們來看一個例子。
有兩個可用的集合:
給定的字典:{'T': 1, 'M': 2, 'E': 3, 'C': 4}
給定的列表:[T, A, B, P, E, L]
因此,最終結果為 [T, E]。
語法
以下語法在示例中使用
keys()
keys() 是 Python 中的內建函式,它返回字典中所有可用鍵的列表。
set()
set() 是 Python 中的內建函式,可用於將多個元素儲存到單個變數中。
intersection()
Python 的內建函式 intersection() 查詢兩個不同集合中的共同匹配項。
append()
append() 是 Python 中的內建函式,它將元素插入列表的末尾。
使用列表推導式
在下面的示例中,程式從名為 dict_keys() 的遞迴函式開始,該函式接受兩個引數 d 和 s,以從給定的輸入字典和字串(即 dict1 和 stng)中獲取值。接下來,它將使用列表推導式技術,首先將鍵設定為變數初始化。使用此變數,迴圈遍歷字典以使用 if 語句檢查條件,即字串是否在給定字典中可用,並返回結果。
示例
def dict_keys(d, s):
return [key for key in d if key in s]
# The input dictionary and string list
dict1 = {'Tablets': 1, 'Mobiles': 2, 'Earbuds': 3, 'Computer': 4}
stng = ['Mouse', 'Hard Drive', 'Pen Drive', 'Earbuds', 'Tablets']
# Calling function
res = dict_keys(dict1, stng)
print("The result of dictionary keys present in a strings list:\n", res)
輸出
The result of dictionary keys present in a strings list: ['Tablets', 'Earbuds']
使用 set() 和 intersection() 函式
在下面的示例中,名為 dict_keys() 的遞迴函式接受兩個引數 d 和 s,以查詢列表中存在的字典鍵。接下來,它將使用函式返回設定一些內建函式,例如 d.keys(),它顯示字典的檢視物件,而 intersection(s) 查詢兩個不同集合之間的共同匹配項。最後,這兩個函式都設定為內建函式 list() 的引數,該函式將以列表的形式返回結果。
示例
def dict_keys(d, s):
return list(set(d.keys()).intersection(s))
# Create the dictionary and string list
dictionary = {'Television': 1, 'Projector': 2, 'White Board': 3}
strings = ['Projector', 'Box', 'Remote']
# Calling function
res = dict_keys(dictionary, strings)
print("The result of dictionary keys present in a strings list:\n", res)
輸出
The result of dictionary keys present in a strings list: ['Projector']
使用簡單的 for 迴圈
在下面的示例中,函式 dict_keys() 中將有兩個引數 d_list 和 s_list,它們將查詢字串列表中存在的字典鍵。然後使用空列表,該列表可用於以列表的形式儲存結果並將其儲存在變數 avl_keys 中。接下來,使用 for 迴圈,變數 k 遍歷變數 d_list(字典列表)。使用 if 語句,條件是給定的輸入字典中是否存在字串。然後使用內建方法 append() 查詢字典鍵並返回到變數 avl_keys 並生成結果。
示例
def dict_keys(d_list, s_list):
avl_keys = []
for k in d_list:
if k in s_list:
avl_keys.append(k)
return avl_keys
# Create the dictionary and string list
dict1 = {'A': 11, 'B': 12, 'C': 13, 'D': 14, 'E': 15, 'F': 16}
stng = ['M', 'N', 'O', 'E', 'G', 'A', 'C', 'P', 'Q', 'R']
# Calling function
res = dict_keys(dict1, stng)
print("The result of dictionary keys present in a strings list:\n", res)
輸出
The result of dictionary keys present in a strings list: ['A', 'C', 'E']
使用 filter 函式
在下面的示例中,我們將從一個接受兩個引數 d 和 s 的遞迴函式開始程式,以從變數 dcn 和 stng 接收字典和字串的原始輸入。接下來,它將使用函式返回使用一些內建函式 lambda- 計算程式的主要操作,filter()- 獲取字典中存在的那些值,以及 list()- 將結果格式化為列表。最後,它將以這種方式生成程式的輸出。
示例
def dict_keys(d, s):
return list(filter(lambda k: k in s, d))
# Create the dictionary and string list
dcn = {'abc': 10, 'bcd': 20, 'mno': 30, 'xyz': 40, 'pqr': 50}
stng = ['ab', 'mno', 'bcs', 'xy']
# Calling Function
res = dict_keys(dcn, stng)
print("The result of dictionary keys present in a strings list:\n", res)
輸出
The result of dictionary keys present in a strings list: ['mno']
結論
在每個示例中,字典和字串都用於設定程式的原始輸入,其中它設定為操作,即字串列表中存在的鍵。使用了各種內建函式,例如 filter()、set()、keys() 等,以在需要時滿足特殊需求。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP