总结
一.匿名函数
没有函数名的函数
函数名 = lambda 形参列表: 返回值
相当于
def 函数名(形参名):
return 返回值
注意: 匿名的本质还是函数, 之前函数中除了定义函数函数的语法以外的内容都适用于匿名
练习:写一个匿名函数判断指定的年是否是闰年
is_leep_year = lambda year: year % 4 == 0 and year % 100 != 0 or year % 400 == 0
print(is_leep_year(1622))
二.函数就是变量
python中定义函数就是定义类型是function的变量, 函数名就是变量名.
普通变量能做的事情, 函数都可以做
func3是一个实参高阶函数 - 如果一个函数的参数是函数,那么这个函数就是实参高阶函数
def func3(x, y, z, m):
# x是长度大于等于2的有序序列
print(x[1])
# y是任何类型的数据
print(y)
# z是字典
print(z['a'])
# m是一个函数
m()
func4是返回值高阶函数 - 如果一个函数的返回值是函数,那么这个函数就是返回值高阶函数
def func4():
# name = '张三'
# return name
def t2():
print('你好!')
return t2
result = func4()() # t2() == None
三. 实参高阶函数
1. max和min
max(序列, key=函数) - 按照key对应的函数指定的规则来获取序列中元素的最大值
函数的要求:1)有且只有一个参数(这个参数指向的是序列中的元素)
2)有一个返回值(比较大小的对象)
2. sorted
sorted(序列, key=函数)
函数的要求:1)有且只有一个参数(这个参数指向的是序列中的元素)
2)有一个返回值(比较大小的对象)
3. max和min的原理
nums = [29, 80, 7, 56, 23]
def temp(item):
print(f'item:{item}')
return item % 10
# result = max(nums, key=temp)
def max_yt(seq: list, *, key=None):
if not key:
m = seq[0]
for x in seq[1:]:
if x > m:
m = x
return m
# 有key的时候
m = seq[0]
for x in seq[1:]:
if key(x) > key(m):
m = x
return m
练习: 将students按学生的分数从大到小排序
students = [
{'name': '小明', 'age': 18, 'score': 92},
{'name': '张三', 'age': 24, 'score': 99},
{'name': '李四', 'age': 30, 'score': 87},
{'name': '王五', 'age': 16, 'score': 62}
]
result = sorted(students, key=lambda item: item['score'], reverse=True)
print(result)
4. map
1) map(函数, 序列) - 将序列中的元素按照函数指定的规则转换成一个新的序列
序列的要求:
a. 有且只有一个参数(指向序列中的每个元素)
b. 需要一个返回值(返回值就是新序列中的元素)
2) map(函数, 序列1, 序列2)
函数的要求:
a. 有且只有两个参数(分别指向后面的两个序列中的元素)
b. 需要一个返回值(返回值就是新序列中的元素)
# 练习:使用map提取所有手机号码的后4位
tel = ['172829393', '1283929333', '167202928888', '17267382014']
result = list(map(lambda item: item[-4:], tel))
print(result)
# 练习:
list1 = [10, 2, 78, 90, 16]
str1 = 'abcde'
list2 = [(10, 20), (3, 7), (9, 10), (103, 56), (1, 2)]
# ['10a20', '2b7', '78c10', '90d56', '16e2']
result = list(map(lambda x, y, z: f'{x}{y}{z[-1]}', list1, str1, list2))
print(result)
5.reduce
调用函数要用 from functools import reduce
reduce(函数, 序列, 初始值) - 将序列中的元素按照函数指定的规则合并成一个数据
函数的要求:
1)有且只有两个参数
第一个参数:第一次指向初始值,从第二次开始指向上一次的计算结果
第二个参数:指向序列中的每个元素
2)描述合并规则
# 练习1:求数字序列中所有元素的乘积
nums = [18, 90, 89, 78, 67]
result = reduce(lambda x, item: x * item, nums, 1)
print(result)
# 练习2:求数字序列中所有个位数的乘积
nums = [18, 90, 89, 78, 67]
result = reduce(lambda x, item: x * item % 10, nums, 1)
print(result)
# 练习3:提取字符串列表中所有的元素的最后一个字符
# ['abc', 'hello', '你好', '123'] -> 'co好3'
list1 = ['abc', 'hello', '你好', '123']
result = reduce(lambda x, item: x+item[-1], list1, '')
print(result)
# 练习4:计算列表中所有数字元素的和
# [18, 'abc', 10.3, True, '你好'] -> 28.3
list1 = [18, 'abc', 10.3, True, '你好']
result = reduce(lambda x, item: x + (item if type(item) in (int, float) else 0), list1, 0)
print(result)
作业
-
已经列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]以下问题使用实参高阶函数来解决
1)获取列表中y坐标最大的点
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ] result = max(points, key=lambda x: x[-1])2)获取列表中x坐标最小的点
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ] result = min(points, key=lambda x: x[0])3)获取列表中距离原点最远的点
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ] result = max(points, key=lambda x: x[0] ** 2 + x[1] ** 2)4)将点按照点到x轴的距离大小从大到小排序
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ] result = sorted(points, key=lambda x: abs(x[1]), reverse = True) -
求列表 nums 中绝对值最大的元素
nums = [12, -21, 54, -23] result = max(nums, key=lambda x: abs(x)) -
已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] 新字典: {'name': '张三', 'age': 18, 'sex': '女'}A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] result = dict(map(lambda x, y: (x, y), A, B)) -
已经三个列表分别表示5个学生的姓名、学科和班号,使用map将这个三个列表拼成一个表示每个学生班级信息的的字典
names = ['小明', '小花', '小红', '老王'] nums = ['1906', '1807', '2001', '2004'] subjects = ['python', 'h5', 'java', 'python'] 结果:{'小明': 'python1906', '小花': 'h51807', '小红': 'java2001', '老王': 'python2004'}names = ['小明', '小花', '小红', '老王'] nums = ['1906', '1807', '2001', '2004'] subjects = ['python', 'h5', 'java', 'python'] result = dict(map(lambda x, y, z: (x, f'{y}{z}'), names, nums, subjects)) -
已经一个列表message, 使用reduce计算列表中所有数字的和(用采用列表推导式和不采用列表推导式两种方法做)
message = ['你好', 20, '30', 5, 6.89, 'hello'] 结果:31.89from functools import reduce message = ['你好', 20, '30', 5, 6.89, 'hello'] # 方法一: result1 = reduce(lambda x, y: x + y if type(y) in [int, float] else x + 0, message, 0) # 方法二: result2 = reduce(lambda x, y: x + y, [i for i in message if type(i) in [int, float]], 0) -
已知一个字典列表中保存的是每个学生各科的成绩,
1)计算并添加每个学生的平均分
2)按照平均分从高到低排序
studens = [ {'name': 'stu1', 'math': 97, 'English': 67, 'Chinese': 80}, {'name': 'stu2', 'math': 56, 'English': 84, 'Chinese': 74}, {'name': 'stu3', 'math': 92, 'English': 83, 'Chinese': 78}, {'name': 'stu4', 'math': 62, 'English': 90, 'Chinese': 88} ] # 计算平均分 # 用函数 def temp(x): x['avg'] = int((x['math'] + x['English'] + x['Chinese']) / 3) return x # 不用函数 result = list(map(temp, studens)) for x in studens: avg = (x['math'] + x['English'] + x['Chinese']) / 3 x.setdefault('avg', int(avg)) studens = [ {'name': 'stu1', 'math': 97, 'English': 67, 'Chinese': 80, 'avg':81}, {'name': 'stu2', 'math': 56, 'English': 84, 'Chinese': 74, 'avg':71}, {'name': 'stu3', 'math': 92, 'English': 83, 'Chinese': 78, 'avg':87}, {'name': 'stu4', 'math': 62, 'English': 90, 'Chinese': 88, 'avg':80} ] # 按照平均分从高到低排序 result = sorted(studens, key=lambda x: x['avg'], reverse=True)