Python 中的 map、reduce 和 filter 函数如何工作?

pythonserver side programmingprogramming更新于 2025/4/29 3:24:17

在本文中,我们将向您展示

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() 函数,以及它们的语法和示例。


相关文章


有用资源