Python map、reduce和filter函数是如何工作的
在本文中,我们将展示
Python的map()、filter()和reduce()函数为语言添加了一丝函数式编程的特点。这三个函数都是方便函数,可以用列表推导式或循环来替代,但对一些问题提供了更优雅和简洁的解决方案。
map()、filter()和reduce()都以相同的方式工作。这些函数接受一个函数和一个元素序列,并返回将接收到的函数应用于序列中每个元素的结果。
map()函数
与reduce()类似,map()函数允许您在可迭代对象中遍历每个项。而map()函数则独立地操作每个项,而不是产生单个结果。
最后,map()函数可以用于在两个或多个列表上执行数学运算。甚至可以用它来操作任何类型的数组。
map()函数的时间复杂度= O (n)
语法
map(function, iterable)
参数
- function - 用于代码中使用的函数。
-
iterable - 这是在代码中进行迭代的值。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个名为 multiplyNumbers 的函数,该函数返回传递给它的数字的乘积结果。
-
在函数内将给定的数字乘以3。
-
使用 map() 函数,通过将函数名和列表作为参数传递给它,对列表的每个元素应用multiplyNumbers()函数。
-
将与3相乘后的结果列表项打印出来。
代码
# creating a function that returns multiplication result
def multiplyNumbers(givenNumbers):
# returning number after multiplying with 3
return givenNumbers*3
# map() function applies the multiplyNumbers function
# for each element of the list
givenNumbers = map(multiplyNumbers, [1, 3, 5, 2, 6, 10])
# Printing the resultant list items
print("Multiplying list elements with 3:")
for element in givenNumbers:
print(element)
输出
执行上述程序后,将生成以下输出−
Multiplying list elements with 3:
3
9
15
6
18
30
filter()函数
filter()函数创建一个新的迭代器,用于从之前创建的迭代器(如列表、元组或字典)中过滤元素。
filter()函数检查序列中是否存在给定条件,然后打印结果。
filter()函数的时间复杂度为O(n)。
语法
filter(function, iterable)
参数
- function − 用于代码中使用的函数。
-
iterable − 在代码中迭代的值。
步骤
以下是执行所需任务的算法/步骤 –
- 创建一个名为 votingAge 的函数,该函数返回投票的合格年龄列表。
-
使用 if 条件语句检查传递给函数的数字是否大于或等于18。
-
如果上述语句为真,则返回该数字。
-
创建一个变量来存储输入列表。
-
使用 filter() 函数,将函数名称和输入列表作为参数传递给它,以从列表中过滤出大于或等于18的年龄。在这里,它将votingAge()函数应用于列表的每个元素,结果仅存储由votingAge()函数返回的列表的值(votingAge()函数在数字大于18时返回该数字)。
-
打印过滤对象
-
使用 list() 函数(返回一个可迭代对象的列表)将上述过滤对象转换为列表并打印出来。
示例
# creating a function that returns the eligibility ages for voting from the
list
def votingAge(givenNumumber):
# checking whether the number is greater than or equal to 18
if givenNumumber>=18:
# returning number
return givenNumumber
# input list
inputList = [3, 20, 18, 6, 14, 25, 19]
# Getting only values of above list which are greater than or equal to 18
result_filterObj = filter(votingAge, inputList)
# printing the filter object
print(result_filterObj)
# converting into a list
print("Eligibility ages for voting from the input list:", list(result_filterObj))
输出
在执行以上程序时,将生成以下输出 –
<filter object at 0x7fcd3ad14280>
Eligibility ages for voting from the input list: [20, 18, 25, 19]
reduce()
在Python中,reduce()函数在列表或其他可迭代数据类型中迭代每个项目,返回一个单一的值。它位于functools库中。这比循环更高效。
语法
reduce(function, iterable)
参数
- function - 代码中要使用的函数。
-
iterable - 在代码中被迭代的值。
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字从functools模块中导入 reduce() 函数。
-
创建一个名为 addNumbers() 的函数,返回列表所有项的和。
-
创建一个变量来存储输入的列表。
-
使用 reduce() 函数,将 addNumbers() 函数和输入的列表作为参数,得到列表所有项的和。
示例
# importing reduce() function from functools module
from functools import reduce
# function that returns the sum of all list items
def addNumbers(x, y):
return x+y
# input list
inputList = [12, 4, 10, 15, 6, 5]
# Print the sum of the list items using reduce() function
print("The sum of all list items:")
print(reduce(addNumbers, inputList))
输出
The sum of all list items:
52
当我们将addNumbers()函数和输入的列表作为参数传递给reduce()函数时,它会取列表的两个元素并将它们相加,得到一个元素,然后取另一个列表元素并再次相加,得到一个元素,以此类推,直到将列表的所有元素相加并返回一个单一的值。
结论
本文涵盖了map(),reduce()和filter()函数,以及它们的语法和示例。
极客笔记