卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16934本站已运行3323

Python - 从字典的值列表中筛选奇数元素

Python - 从字典的值列表中筛选奇数元素

字典是Python中一种流行的数据类型,它有一个键值对,并且不允许重复。为了过滤奇数元素,它有一些内置函数,如 items()、filter()、lambda,并且 list() 将用于从字典中的值列表中过滤奇数元素。列表中的奇数元素是那些不能被 2 整除的元素。

例如 -

给定列表,[10, 22, 21, 19, 2, 5]

从列表中过滤奇数元素后:

最终结果变成[10, 22, 2](这些是能被整数2整除的元素)。

语法

示例中使用以下语法 -

'
items()

这是一个内置方法,可用于返回视图对象。该对象由带有值对的键组成。

'
filter()

Python的filter()元素用于根据特定条件过滤元素。

'
lambda

函数 lambda 提供了使用 lambda 关键字声明简短匿名函数的快捷方式。当使用 def 关键字声明时,lambda 函数会起作用。

'
list()

list() 是内置的 Python,它创建列表对象,它接受可迭代构造并将其转换为列表。

'
x % 2 != 0
or
x % 2 == 1

上述两种表示都说明了从值列表中过滤奇数元素的逻辑。

通过 if-else 语句使用字典理解和列表理解

该程序使用字典理解,通过过滤奇数元素将原始字典转换为新字典。可以通过使用带有 if-else 语句的列表压缩来过滤这个奇怪的元素。

示例

在以下示例中,使用名为odd_element 的函数启动程序,该函数接受名为dictionary 的参数。相同的参数与理解技术一起使用,即 listdictionary 以及 if-statement 来设置值列表中奇数元素的过滤器字典。然后创建字典的值列表并将它们存储在名为 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() 迭代字典项的键和值。然后它将使用嵌套的内置函数(如 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的参数。最后,使用接受变量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\': []}

将字典理解和 Filter() 与 Lambda 函数结合使用

该程序使用字典理解来帮助将一本字典转换为新形式的字典。 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 行内解决问题。当我们想要通过分配特定条件来过滤数据时,通常会使用程序目的。

卓越飞翔博客
上一篇: Python是区分大小写的还是不区分大小写的?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏