Python 内置高阶函数

Python 提供了许多内置的高阶函数,以下是几个常用的:

1. map() 函数

将一个函数应用于一个或多个可迭代对象的所有元素,并返回一个迭代器

map(function, iterable, ...)
  • function:一个函数,可以是普通函数或 lambda 函数。
  • iterable:一个或多个可迭代对象(如列表、元组等)。
  • 返回值:返回一个迭代器,包含将 function 应用于 iterable 中每个元素的结果。

示例 1:对列表中的每个元素平方

# 使用 map 将列表中的每个元素平方
numbers = [1, 2, 3, 4]
squared = map(lambda x: x ** 2, numbers)
print(list(squared))  # 输出: [1, 4, 9, 16]

示例 2:对两个列表的元素相加

# 使用 map 对两个列表的元素相加
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = map(lambda x, y: x + y, list1, list2)
print(list(result))  # 输出: [5, 7, 9]

2. reduce() 函数

将一个函数累积地应用于可迭代对象的所有元素,最终返回一个值。reduce() 不是内置函数,而是位于 functools 模块中。

from functools import reduce

reduce(function, iterable, initializer=None)
  • function:一个接受两个参数的函数,用于累积计算。
  • iterable:一个可迭代对象(如列表、元组等)。
  • initializer:初始值(可选,如果不传第一个参数就是迭代对象的第一个元素;如果传了,就作为第一参数)。
  • 返回值:返回最终的累积结果。

函数是接受两个参数,工作流程如下:

  1. 先把可迭代对象的【头两个元素】作为参数调用传入的函数,得到一个返回值
  2. 再把【返回值】和【第三个元素】作为参数调用传入的函数,得到一个新的返回值
  3. 再把新的【返回值】和【第四个元素】作为参数调用传入的函数,再得到一个新的返回值
  4. ...
  5. 以此类推,直到最后一个元素也作为参数传入函数,得到最终的返回值,返回这个最终的返回值

示例 1:计算列表中所有元素的和

from functools import reduce

# 使用 reduce 计算列表中所有元素的和
numbers = [1, 2, 3, 4]
total = reduce(lambda x, y: x + y, numbers)
print(total)  # 输出: 10

示例 2:计算列表中所有元素的乘积

from functools import reduce

# 使用 reduce 计算列表中所有元素的乘积
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product)  # 输出: 24

示例 3:使用初始值

from functools import reduce

# 使用 reduce 计算列表中所有元素的和,初始值为 10
numbers = [1, 2, 3, 4]
total = reduce(lambda x, y: x + y, numbers, 10)
print(total)  # 输出: 20

3. filter() 函数

根据条件过滤可迭代对象中的元素,并返回一个迭代器。

filter(function, iterable)
  • function:一个返回布尔值的函数,可以是普通函数或 lambda 函数。
  • iterable:一个可迭代对象(如列表、元组等)。
  • 返回值:返回一个迭代器,包含 iterable 中满足 function 条件的元素。

示例 1:过滤出列表中的偶数

# 使用 filter 过滤出列表中的偶数
numbers = [1, 2, 3, 4, 5, 6]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens))  # 输出: [2, 4, 6]

示例 2:过滤出长度大于 5 的字符串

# 使用 filter 过滤出长度大于 5 的字符串
words = ["apple", "banana", "cherry", "date"]
long_words = filter(lambda x: len(x) > 5, words)
print(list(long_words))  # 输出: ['banana', 'cherry']

4. sorted() 函数

对可迭代对象进行排序,并返回一个新的列表。

sorted(iterable, key=None, reverse=False)
  • iterable:一个可迭代对象(如列表、元组等)。
  • key:一个函数,用于指定排序的依据(默认是元素本身)。
  • reverse:是否逆序排序(默认是 False,即升序)。
  • 返回值:返回一个新的排序后的列表。

示例 1:按字母顺序排序

# 使用 sorted 对列表按字母顺序排序
words = ["banana", "apple", "cherry"]
sorted_words = sorted(words)
print(sorted_words)  # 输出: ['apple', 'banana', 'cherry']

示例 2:按字符串长度排序

# 使用 sorted 按字符串长度排序
words = ["apple", "banana", "cherry"]
sorted_words = sorted(words, key=lambda x: len(x))
print(sorted_words)  # 输出: ['apple', 'cherry', 'banana']

示例 3:逆序排序

# 使用 sorted 逆序排序
numbers = [3, 1, 4, 1, 5, 9]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)  # 输出: [9, 5, 4, 3, 1, 1]

示例 4:字典排序

lst = [
  {"name": "Milk", "age": 20},
  {"name": "Rose", "age": 22},
  {"name": "Marry", "age": 19},
]

# 输出 [{'name': 'Rose', 'age': 22}, {'name': 'Milk', 'age': 20}, {'name': 'Marry', 'age': 19}]
print(sorted(lst, key=lambda item: item["age"], reverse=True))

5. 总结

Python 内置的高阶函数非常强大,以下是它们的核心特点:

函数 作用 参数 返回值
map() 将函数应用于可迭代对象的每个元素 function, iterable, ... 迭代器
filter() 过滤出满足条件的元素 function, iterable 迭代器
sorted() 对可迭代对象进行排序 iterable, key, reverse 排序后的列表
reduce() 累积计算可迭代对象的元素 function, iterable, initializer 最终结果
posted @ 2025-03-09 11:18  CyrusHuang  阅读(60)  评论(0)    收藏  举报