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循环和filter()与Lambda函数
该程序使用for循环,通过内置的items()方法迭代遍历字典项的key和value。然后,它将使用嵌套的内置函数如list()、filter()和lambda来从字典的列表中删除奇数元素。
示例
在以下示例中,我们将使用for循环遍历包含键和值列表的变量字典。为了过滤奇数元素,它将使用三个嵌套的内置函数,即list()、filter()和lambda() [该函数设置条件x%2 != 0,检查给定的值列表整数是否能被2整除]并将其存储在变量filtered_dictionary中。过滤奇数元素后,将filtered_dictionary的值设置为filtered_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的参数。最后,使用print函数接受变量f_dictionary来获取结果。
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]}
结论
根据字典中的值列表过滤奇数元素,我们讨论了解决这个问题陈述的各种方法。以上示例大多数都使用完整的技术,在1-2行代码内通过使用某种方法、循环或条件语句来解决问题。程序的目的通常是在想要根据特定条件筛选数据时使用。