Python 程式提取特定值型別的鍵
Python 中,一種更常見地稱為關聯陣列的資料結構的實現是**字典**。字典由一組鍵值對組成。每個鍵值組合對應一個鍵及其對應的值。
在本文中,我們將學習如何在 Python 中提取具有特定值型別的鍵。
使用的方法
以下是完成此任務的各種方法 -
使用 for 迴圈和 isinstance() 方法
使用列表推導式和 isinstance() 方法
使用 keys() 和 type() 方法
示例
假設我們已經獲取了一個**輸入字典**。我們現在將
輸入
inputDict = {'hello': 5, 'tutorials': 'hi', 'users': 2, 'codes': 'user', 'python': [1, 2]} extractType = str
輸出
['tutorials', 'codes']
在上述輸入字典中,具有 str(字串)型別的值為**'hi'**和**'user'**。這些值的對應鍵分別為**'tutorials'**和**'codes'**。因此提取這些鍵。
使用 for 迴圈和 isinstance() 方法
isinstance() 函式
如果給定物件是指定型別,則 isinstance() 函式返回**True**;否則,它返回**False**。
如果 type 引數是**元組**,則如果物件是元組中型別的其中之一,則返回**True**。
語法
isinstance(object, type)
引數
**object** - 要檢查的物件,以檢視它是否屬於該類。
**type** - 型別或類,或型別和/或類的元組
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
建立一個變數來儲存一個包含多種資料型別的**輸入字典**。
列印輸入字典。
建立另一個變數來儲存要提取的輸入資料型別。
初始化一個空列表來儲存指定輸入型別的結果鍵。
使用**for 迴圈**遍歷輸入字典的鍵和值,使用**items() 函式**。
使用**if 條件**語句檢查當前值型別是否等於使用**isinstance()**函式的輸入提取型別。
如果條件為**true**,則使用**append() 函式**(將元素新增到列表的末尾)將相應的鍵附加到結果鍵列表。
列印具有指定輸入型別的輸入字典的結果鍵。
示例
以下程式使用 for 迴圈和 isinstance() 方法從輸入字典返回給定輸入值型別的鍵 -
# input dictionary having multiple datatypes inputDict = {'hello': 5, 'tutorials': 'hi', 'users': 2, 'codes': 'user', 'python': [1, 2]} # printing input dictionary print("Input dictionary:\n", inputDict) # input datatype to be extracted extractType = str # storing resultant keys of input type resultantKeys = [] # traversing through keys, values of input dictionary for k, v in inputDict.items(): # checking whether the current value type is equal to the input data type if isinstance(v, extractType): # appending that corresponding key to the resultant keys list resultantKeys.append(k) # printing the resultant keys of the dictionary having the specified input type print("Resultant keys of input dictionary with 'string' type:\n", resultantKeys)
輸出
執行後,上述程式將生成以下輸出 -
Input dictionary: {'hello': 5, 'tutorials': 'hi', 'users': 2, 'codes': 'user', 'python': [1, 2]} Resultant keys of input dictionary with 'string' type: ['tutorials', 'codes']
使用列表推導式和 isinstance() 方法
當您希望根據現有列表的值構建新列表時,列表推導式提供了更短/簡潔的語法。
示例
以下程式使用列表推導式和 isinstance() 方法從輸入字典返回給定輸入值型別的鍵 -
# input dictionary having multiple datatypes inputDict = {'hello': 5, 'tutorials': 'hi', 'users': 2, 'codes': 'user', 'python': [1, 2]} # printing input dictionary print("Input dictionary:\n", inputDict) # input datatype to be extracted extractType = str # Using list comprehension to extract keys with string datatype resultantKeys = [k for k, v in inputDict.items() if isinstance(v, extractType)] # printing the resultant keys of the dictionary having the specified input type print("Resultant keys of input dictionary with 'string' type:\n", resultantKeys)
輸出
執行後,上述程式將生成以下輸出 -
Input dictionary: {'hello': 5, 'tutorials': 'hi', 'users': 2, 'codes': 'user', 'python': [1, 2]} Resultant keys of input dictionary with 'string' type: ['tutorials', 'codes']
方法 3 使用 keys() 和 type() 方法
**keys() 方法** - dict.keys() 方法提供一個檢視物件,該物件按插入順序顯示字典中所有鍵的列表。
**type()** 函式(返回物件的型別)。
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
使用**for 迴圈**遍歷使用**keys()**函式的輸入字典的鍵
使用**if 條件**語句檢查當前值的型別是否等於使用 type() 函式的輸入提取型別。
如果條件為**true**,則使用**append()**函式(將元素新增到列表的末尾)將當前鍵附加到結果鍵列表。
列印具有指定輸入型別的輸入字典的結果鍵。
示例
以下程式使用 keys() 和 type() 函式從輸入字典返回給定輸入值型別的鍵 -
# input dictionary having multiple datatypes inputDict = {'hello': 5, 'tutorials': 'hi', 'users': 2, 'codes': 'user', 'python': [1, 2]} # printing input dictionary print("Input dictionary:\n", inputDict) # input datatype to be extracted extractType = str # storing resultant keys of input type resultantKeys = [] # traversing through keys of input dictionary for k in inputDict.keys(): # checking whether the current value type of that corresponding key # is equal to the input extract type if type(inputDict[k]) is extractType: # appending the current key to the resultant list resultantKeys.append(k) # printing the resultant keys of the dictionary having the specified input type print("Resultant keys of input dictionary with 'string' type:\n", resultantKeys)
輸出
執行後,上述程式將生成以下輸出 -
Input dictionary: {'hello': 5, 'tutorials': 'hi', 'users': 2, 'codes': 'user', 'python': [1, 2]} Resultant keys of input dictionary with 'string' type: ['tutorials', 'codes']
結論
本文介紹了三種不同的提取具有特定值型別的鍵的方法。此外,我們還學習瞭如何使用 type() 函式確定變數的型別以及如何使用 isinstance() 方法比較兩種資料型別。