Python - 字典中過濾值列表中的奇數元素
字典是 Python 中一種常用的資料型別,它以鍵值對的形式儲存資料,並且不允許重複的鍵。為了過濾奇數元素,它使用了一些內建函式,如 items()、filter()、lambda 和 list() 來過濾字典中值列表的奇數元素。列表中的奇數元素是指不能被 2 整除的元素。
例如 -
給定的列表:[10, 22, 21, 19, 2, 5]
從列表中過濾奇數元素後
最終結果變為 [10, 22, 2](這些是能被整數 2 整除的元素)。
語法
以下語法在示例中使用 -
items()
這是一種內建方法,可用於返回檢視物件。該物件包含鍵值對。
filter()
Python 的 filter() 元素用於根據特定條件過濾元素。
lambda
函式 lambda 提供了一種使用 lambda 關鍵字宣告簡短匿名函式的快捷方式。lambda 函式在使用 def 關鍵字宣告時表現相同。
list()
list() 是 Python 的內建函式,用於建立列表物件,它接受一個可迭代的結構並將其轉換為列表。
x % 2 != 0 or x % 2 == 1
以上兩種表示都說明了從值列表中過濾奇數元素的邏輯。
使用字典推導和列表推導以及 if-else 語句
該程式使用字典推導將原始字典轉換為新字典,方法是過濾奇數元素。可以使用帶有 if-else 語句的列表推導來過濾這些奇數元素。
示例
在以下示例中,程式以名為 odd_element 的函式開始,該函式接受名為 dictionary 的引數。相同的引數與推導技術(即 list 和 dictionary)以及 if 語句一起工作,以設定從字典中值列表過濾奇數元素的過濾器。然後建立字典的值列表並將它們儲存在名為 dictionary 的變數中。接下來,使用呼叫函式並將引數名稱作為包含鍵值對的字典傳遞,並將它們儲存在變數 filter_dict 中。最後,我們藉助變數 filter_dict 列印結果。
def odd_elements(dictionary):
return {key: [x for x in value if x % 2 != 0] for key, value in dictionary.items()}
# Create the dictionary
dictionary = {'A': [2, 4, 16, 19, 17], 'B': [61, 71, 90, 80, 10], 'C': [11, 121, 13, 14, 15]}
filter_dict = odd_elements(dictionary)
print("Filter odd elements from the value lists in dictionary:\n", filter_dict)
輸出
Filter odd elements from the value lists in dictionary:
{'A': [19, 17], 'B': [61, 71], 'C': [11, 121, 13, 15]}
使用 for 迴圈和帶有 Lambda 函式的 Filter()
該程式使用 for 迴圈,它將使用內建方法 items() 遍歷字典的鍵和值。然後,它將使用巢狀的內建函式,如 list()、filter() 和 lambda 來從字典中的列表中刪除奇數元素。
示例
在以下示例中,我們將使用 for 迴圈遍歷包含鍵和值列表的變數 dictionary。為了過濾奇數元素,它將使用三個巢狀的內建函式,即 list()、filter() 和 lambda()[此函式將條件設定為 x%2 != 0,它將檢查給定的值列表整數是否可被 2 整除] 並將其儲存在變數 filtered_dictionary 中。過濾奇數元素後,filtered_dictionary 的值將設定為 filtered_dictionary。然後建立包含鍵和值列表的字典,並將其儲存在變數 dictionary 中。現在,此變數設定為名為 odd_element() 的呼叫函式的引數,並存儲在變數 filter_dict() 中。
def odd_elements(dictionary):
filtered_dictionary = {}
# for loop
for key, value in dictionary.items():
# Using filter() with lambda
filtered_values = list(filter(lambda x: x % 2 != 0, value))
filtered_dictionary[key] = filtered_values
return filtered_dictionary
# create the dictionary
dictionary = {'A': [1, 2, 3, 4, 5], 'B': [6, 7, 8, 9, 10], 'C': [11, 12, 13, 14, 15], 'D': [16, 17, 18, 19, 20]}
filter_dict = odd_elements(dictionary)
print("Filter odd elements from the value lists in dictionary:\n", filter_dict)
輸出
Filter odd elements from the value lists in dictionary:
{'A': [1, 3, 5], 'B': [7, 9], 'C': [11, 13, 15], 'D': [17, 19]}
使用 for 迴圈和列表推導
該程式使用 for 迴圈遍歷字典和鍵,使用內建方法 items(),然後它將使用 for 迴圈和 if 語句在一行中表示列表推導。
示例
在以下示例中,程式首先定義名為 filter_odd_elements() 的函式,該函式接受名為 dictionary 的引數以訪問其值。接下來,在變數 filter_dictionary 中建立一個空字典,該字典稍後將儲存新字典作為結果。然後,它將使用 for 迴圈遍歷字典的每個值列表。接下來使用 for 和 if 語句的列表推導,並將其儲存在變數 filter_values 中。在 filter_dictionary[key] 中交換相同的變數。然後返回 filter_dictionary,它具有過濾後的結果,沒有奇數元素。建立一個具有值列表的字典,並將其儲存在變數 dict 中。名為 f_dictionary 的新變數儲存遞迴函式以傳遞名為 dict 的引數。最後,使用接受變數 f_dictionary 的 print 函式獲取結果。
def filter_odd_elements(dictionary):
filter_dictionary = {}
for key, value in dictionary.items():
# List Comprehension
filter_values = [x for x in value if x % 2 != 0]
filter_dictionary[key] = filter_values
return filter_dictionary
# Creation of dictionary
dict = {'A': [307, 907], 'B': [100, 200], 'C': [110, 120]}
# use the calling function
f_dictionary = filter_odd_elements(dict)
print("Filtration of odd elements from dictionary value list:\n", f_dictionary)
輸出
Filtration of odd elements from dictionary value list:
{'A': [307, 907], 'B': [], 'C': []}
使用字典推導和帶有 Lambda 函式的 Filter()
該程式使用字典推導,有助於將一個字典轉換為新的字典形式。名為 filter() 的方法使用 lambda 函式從字典的值列表中消除奇數元素。
示例
在以下示例中,我們將展示字典推導如何使用三種方法根據值列表中的奇數元素過濾器設定邏輯,並使用 for 迴圈遍歷字典的每個鍵和值。
def odd_elements(dictionary):
return {key: list(filter(lambda x: x % 2 == 1, value)) for key, value in dictionary.items()}
# Create the dictionary
dict_1 = {'I': [1, 2, 3, 4, 5], 'II': [6, 7, 8, 9, 10], 'III': [11, 12, 13, 14, 15]}
filter_dict = odd_elements(dict_1)
print("ODD NUMBER FILTRATION IN DICTIONARY VALUES:\n", filter_dict)
輸出
ODD NUMBER FILTRATION IN DICTIONARY VALUES:
{'I': [1, 3, 5], 'II': [7, 9], 'III': [11, 13, 15]}
使用字典推導和列表推導
該程式使用遞迴函式,該函式透過使用 return 語句返回兩種推導技術。
示例
在以下示例中,我們將使用程式中的遞迴函式從字典的值中過濾掉奇數元素,並返回一個具有相同鍵和過濾後值的新字典。
def odd_elements(dictionary):
return {key: [x for x in value if x % 2 == 1] for key, value in dictionary.items()}
# create the dictionary and store the value by odd and even in the list
dictionary = {'list1': [100, 200, 300, 499, 599], 'list2': [699, 799, 899, 900, 1000]}
filter_dict = odd_elements(dictionary)
print("ODD NUMBER FILTRATION IN DICTIONARY VALUES:\n", filter_dict)
輸出
ODD NUMBER FILTRATION IN DICTIONARY VALUES:
{'list1': [499, 599], 'list2': [699, 799, 899]}
結論
我們討論了基於從字典中值列表中過濾奇數元素來解決此問題陳述的各種方法。以上所有示例大多使用綜合技術,透過使用某些方法、迴圈或條件語句在一兩行內解決問題。程式的用途通常在我們希望透過分配特定條件來過濾資料時使用。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP