Python 中過濾非 None 字典鍵


Python 字典是以下 4 種資料型別中最流行的一種。字典定義了鍵值對,並且不允許重複。值可以是字串或整數。有時,在處理字典時,它會有一些空值,可以使用 None 值來填充。例如,當我們處理機器學習資料集時,會發現某些行是空的,在執行特定任務時,可以使用 None 值來填充。在 Python 中,我們有一些內建函式,如 items() 和 lambda,可以用來過濾 Python 中的非 None 字典鍵。

讓我們來看一個例子:

The given dictionary, {‘A’: 11, ‘B’: None, ‘C’: 29, ‘D’: None}
Then the final result becomes {‘A’: 11, ‘C’: 29}

語法

以下語法在示例中使用:

items()

items() 是 Python 中一個內建方法,允許它遍歷字典中的鍵和值。

lambda

函式 lambda 提供了一種使用 lambda 關鍵字宣告簡短匿名函式的快捷方式。lambda 函式的行為與使用 def 關鍵字宣告時相同。

使用字典推導式

該程式設定鍵和值,透過使用字典推導式將一個字典轉換為另一個字典。然後使用名為 items() 的內建函式和條件 if 語句從列表中過濾 None 值。

示例

在下面的示例中,它將使用建立新字典的字典推導式。在 return 語句中,它使用 items() 方法遍歷字典中的鍵值對。然後檢查每個鍵值對,如果值不是 None,則將鍵值對包含在新字典中。然後建立原始字典以設定其值。接下來,使用呼叫函式來呼叫該函式,它被稱為遞歸併將其儲存在變數 filtered_dict 中。最後,我們藉助變數 filtered_dict 列印結果。

def filter_none(dictionary):
   return {key: value for key, value in dictionary.items() if value is not None}
# Create the dictionary
my_dict = {'a': 10, 'b': None, 'c': 30, 'd': None, 'e': 50}
filtered_dict = filter_none(my_dict)
print("After filter of Non-none dictionary key:\n", filtered_dict)

輸出

 After filter of Non-none dictionary key:
 {'a': 10, 'c': 30, 'e': 50}

使用 filter() 函式

該程式使用 filter() 函式,該函式接受兩個引數作為內建函式,即 lambda 和 items(),用於過濾 Python 中的非 None 字典鍵。

示例

在下面的示例中,我們將展示名為 filter() 的內建函式以及 lambda 來從字典中刪除值。通常,此返回值函式充當列表推導式。然後它將建立輸入字典以設定 none 值,接下來它將使用呼叫函式傳遞輸入字典並獲取結果。

def filter_non_none_dict_filter(dictionary):
   return dict(filter(lambda item: item[1] is not None, dictionary.items()))

my_dict = {'I': 100, 'II': None, 'III': 300, 'IV': None, 'V': None}
filtered_dict = filter_non_none_dict_filter(my_dict)
print("After the filter of Non-none dictionary key:\n", filtered_dict)

輸出

 After the filter of Non-none dictionary key:
 {'I': 100, 'III': 300}

使用 for 迴圈和字典

該程式使用 for 迴圈迭代字典項的鍵和值,並使用 if 語句根據 None 檢查條件,這將過濾 Python 中的非 None 字典鍵。

示例

在下面的示例中,從名為 filter_dict_loop 的函式開始程式,該函式接受名為 dictionary 的引數以接收其值。然後在變數 filter_dict 中建立空字典,該變數將僅儲存鍵的整數值。現在使用 for 迴圈,該迴圈使用方法 items() 迭代字典的鍵和值。使用 if 語句,它將檢查是否發現值為 none,然後它將自動從字典中刪除 key : value。接下來,返回函式。繼續建立包含鍵中整數和 none 值組合的字典,並將其儲存在變數 my_dict 中。函式呼叫用於呼叫函式以將引數作為名為 f_dict 的變數傳遞。最後,我們藉助變數 f_dict 列印結果。

def filter_dict_loop(dictionary):
   filtered_dict = {}
   for key, value in dictionary.items():
      if value is not None:
         filtered_dict[key] = value
   return filtered_dict
# Create the dictionary 
my_dict = {'A': 91, 'B': None, 'C': 33, 'D': 78, 'E': 5}
f_dict = filter_dict_loop(my_dict)
print("After the filter of Non-none dictionary key:\n", f_dict)

輸出

After the filter of Non-none dictionary key:
 {'A': 91, 'C': 33, 'D': 78, 'E': 5}

使用帶有條件三元表示式的字典推導式

該程式使用字典推導式將原始字典轉換為過濾後的字典,條件三元表示式是在一行中編寫 if-else 語句的一種方式。它通常用於字典推導式中以有條件地包含鍵值對。

示例

在下面的示例中,透過定義一個名為 filter_dict_conditional 的遞迴函式開始程式,該函式接受字典引數以訪問其所有鍵和值元素。然後它透過刪除非 None 字典鍵來返回函式。接下來,它將建立字典以設定鍵及其值,並將其儲存在變數 my_dict 中。現在使用呼叫函式將引數作為名為 my_dict 的變數傳遞,並將其儲存在變數 filtered_dict 中。最後,藉助變數 filtered_dict 列印結果。

def filter_dict_conditional(dictionary):
   return {key: value for key, value in dictionary.items() if value if not None}

my_dict = {'ABC': 11, 'DEF': None, 'GHI': 13, 'JKL': None, 'MNO': 15}
filtered_dict = filter_dict_conditional(my_dict)
print("After the filter of Non-none dictionary key:\n", filtered_dict)

輸出

 After the filter of Non-none dictionary key:
 {'ABC': 11, 'GHI': 13, 'MNO': 15}

結論

我們討論了理解 Python 中非 None 字典鍵過濾的各種方法。可以使用 items()、lambda 和 filter() 等各種內建方法來刪除非 None 值。此程式涉及機器學習資料集的實際示例,透過根據字典提供特定條件,將空資料值填充為 none。

更新於: 2023-07-17

456 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告