python-16-常见高级函数
一.说明
python中的基础系列中的关于函数部分,还有一些特殊函数叫高级函数,在真实项目中开发使用频率较高,说句人话,就是真香 实用!。
二.定义
在Python中,高阶函数是指接受其他函数作为参数或返回一个函数的函数。高阶函数使得我们能够以更灵活和简洁的方式处理函数。
三.说明
-
**map()**:
map()
函数用于将指定函数应用于给定可迭代对象(如列表、元组等)的每个元素,并返回一个迭代器;# 定义一个函数,计算平方
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
# 将迭代器转换为列表
print(list(squared_numbers)) # 输出: [1, 4, 9, 16, 25] -
filter()
filter()
函数用于筛选可迭代对象中的元素,返回符合条件的元素;# 定义一个函数,检查是否为偶数
def is_even(x):
return x % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(is_even, numbers)
# 将迭代器转换为列表
print(list(even_numbers)) # 输出: [2, 4, 6] -
reduce()
reduce()
函数来自functools
模块,用于对可迭代对象的元素进行累积操作;1.基本语法
from functools import reduce
result = reduce(function, iterable[, initializer])function
:一个接收两个参数的函数,用于对元素进行处理。iterable
:一个可迭代对象(如列表、元组等),其元素将会被function
处理。initializer
(可选):用于指定初始值。如果提供,累积的开始值将是这个值。2.工作原理
reduce()
会对iterable
中的元素进行如下操作:. 先取出前两个元素,将其传入
function
。 . 将function
的返回值与下一个元素一起传入function
。 . 重复这个过程,直到所有元素都被处理完,最终返回的结果就是累积值。示例:
-
计算和
from functools import reduce
# 定义一个函数,计算两个数的和
def add(x, y):
return x + y
numbers = [1, 2, 3, 4, 5]
result = reduce(add, numbers)
print(result) # 输出: 15 (1 + 2 + 3 + 4 + 5) -
计算乘积
from functools import reduce
# 定义一个函数,计算两个数的乘积
def multiply(x, y):
return x * y
numbers = [1, 2, 3, 4]
result = reduce(multiply, numbers)
print(result) # 输出: 24 (1 * 2 * 3 * 4) -
使用
initializer
如果提供
initializer
,它会作为初始值参与计算。例如,计算和时从10
开始:from functools import reduce
numbers = [1, 2, 3]
result = reduce(add, numbers, 10)
print(result) # 输出: 16 (10 + 1 + 2 + 3) -
使用 Lambda 表达式
from functools import reduce
# 使用 lambda 表达式计算和
result = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
print(result) # 输出: 15
# 使用 lambda 表达式计算乘积
result = reduce(lambda x, y: x * y, [1, 2, 3, 4])
print(result) # 输出: 24
-
-
sorted()
sorted()
函数用于对可迭代对象进行排序,可以接受一个排序函数作为参数注意
sorted()
函数并不会改变原可迭代对象,而是返回一个新的可迭代对象def sort_by_length(s):
return s[quantity]
words = [{'name':apple,'quantity':10},
{'name':banana,'quantity':25},
{'name':cherry,'quantity':8},
{'name':date,'quantity':16}]
sorted_words = sorted(words, key=sort_by_length)
print(sorted_words)
'''
输出: [
{'name': 'cherry', 'quantity': 8},
{'name': 'apple', 'quantity': 10},
{'name': 'date', 'quantity': 16},
{'name': 'banana', 'quantity': 25}
]
'''
# 定义一个函数,按照字符串长度排序
def sort_by_length(s):
return len(s)
words = [apple, banana, cherry, date]
sorted_words = sorted(words, key=sort_by_length)
print(sorted_words) # 输出: ['date', 'apple', 'banana', 'cherry'] -
any()和all()
-
any()
:any()函数返回 True,如果可迭代对象中至少有一个元素为真(即不为False
、None
、0
、空字符串或空容器),否则返回 False -
all()
:all()函数返回 True,如果可迭代对象中的所有元素都是“真”的(即每个元素都不为False
、None
、0
、空字符串或空容器),否则返回 False -
语法
any(iterable)
all(iterable)
# iterable:一个可迭代对象,其中的元素会被检查。 -
示例
注意为什么使用map,就是通过map 对可迭代元素进行处理使其返回 true、false的列表
numbers = [1, 2, 3, 4, 5]
# 检查是否有偶数
has_even = any(map(lambda x: x % 2 == 0, numbers))
print(has_even) # 输出: True
# 检查是否所有数都是正数
all_positive = all(map(lambda x: x > 0, numbers))
print(all_positive) # 输出: True
-
-
zip()
zip()函数用于将多个可迭代对象打包成一个元组的迭代器,常用于并行迭代。
names = [Alice, Bob, Charlie]
ages = [25, 30, 35]
#使用 zip() 组合两个列表
combined = zip(names, ages)
#转换为列表并打印
print(list(combined)) # 输出: [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
四.总结
常用的高级函数我就介绍到这里,这些高级函数,在大多数编程语言中都有类似,有其他语言开发经验应该都知道怎么使用!确实使用频率比较高!大家还要多多复习!
创作整理不易,请大家多多关注 多多点赞,有写的不对的地方欢迎大家补充,我来整理,再次感谢!