1.实参高阶函数
1.1函数的本质
python中定义函数其实就是在定义一个类型是function的变量, 函数名就是变量名;
变量能做的函数都可以做
a = 10
b = 'abc'
c = [1, 29, 8]
d = {'a': 23, 'b': 100}
e = lambda x: x*2 # e 是变量
"""
def e(x):
return x*2
"""
def func1(): # func1 是变量
print('函数')
print(type(d), type(e), type(func1)) # <class 'dict'> <class 'function'> <class 'function'>
2.变量的常用操作
a = 10
x1 = a
print(x1, x1 + 10) # 10 20
x2 = func1
x2() # 函数
# 2)
list1 = [a, 100]
print(list1, list1[0] * 10) # [10,100] 100
list1 = [func1, 100]
print(list1, list1[0]()) # [<function func1 at 0x7f824a1ff700>, 100] None
# 3)
a = 100
print(a) # 100
func1 = 100
print(func1) # 100
# 4)
def func2(x):
print(x)
def func1(): # func1 是变量
print('函数')
a = 10
func2(a) # 10
func2(func1) # <function func1 at 0x7f824a1ff820>
# 5)
def func3():
aa = 10
return aa
def func4():
def fn():
print('小函数')
return fn
func4()() # fn() 小函数
def test(x):
x(10)
test(func2) # 10
2.高阶函数
实参高阶函数 - 如果一个函数的参数是函数,那么这个函数就是实参高阶函数 (掌握怎么使用系统提供的实参高阶函数
返回值高阶函数 - 如果一个函数的返回值是函数,那么这个函数就是返回值高阶函数(会写装饰器)
1.2实参高阶函数
1.常用实参高阶函数:max、min、sorted、map、reduce
-
max 和 min、sorted
max(序列, key=函数)
sorted(序列, key=函数)
列表.sort(key=函数)
函数的要求:
a. 有且只有一个参数,这个参数指向的数前面序列中的每个元素
b. 需要一个返回值,返回值决定求最大值的时候比较的对象
nums = [29, 87, 65, 9, 25]
print(max(nums))
# 求列表nums中个位数最大的元素: 19
nums = [28, 87, 65, 19, 25]
result = max(nums, key=lambda item: item % 10)
print(result) # 19
# 求列表nums中十位数最大的元素:
nums = [28, 607, 65, 19, 85]
result = max(nums, key=lambda item: item // 10 % 10)
print(result) # 85
nums = [28, 607, -650, 19, -85]
# result = max(nums, key=lambda item: item**2)
result = max(nums, key=lambda item: item if item > 0 else -item)
print(result) # -650
# 练习:求列表中各个位上和最大的元素
# 10, 13, 11, 10, 16
nums = [28, 607, 65, 19, 88]
# 方法一:
# def sum1(item):
# s = 0
# for x in str(item):
# s += int(x)
# return s
# result = max(nums, key=sum1)
# 方法二
result = max(nums, key=lambda item: sum([int(x) for x in str(item)]))
print(result)
# 练习:用min获取students中年龄最小的学生
students = [
{'name': '小明', 'age': 18, 'score': 98, 'tel': '187283822'},
{'name': '小红', 'age': 30, 'score': 99, 'tel': '187283827'},
{'name': 'Tom', 'age': 25, 'score': 87, 'tel': '187283821'},
{'name': 'Bob', 'age': 19, 'score': 65, 'tel': '187283820'}
]
result = min(students, key=lambda stu: stu['age'])
print(result)
result = max(students, key=lambda stu: stu['score'])
print(result)
# 将列表中的元素按照个位数的大小从小到大排序
nums = [28, 87, 65, 19, 25]
result = sorted(nums, key=lambda item: item % 10)
print(result) # [65, 25, 87, 28, 19]
students.sort(key=lambda stu: stu['tel'][-1])
print(students)
# 求nums数字数值最大的元素
nums = [23, '38', '2893', 89]
result = max(nums, key=lambda item: int(item))
print(result)
2.map函数 - 将原序列中元素按照指定的标准进行变换,产生一个新的序列
1)map(函数, 序列)
函数的要求:
a.有且只有一个参数,这个参数指向的是后面这个序列每个元素
b.需要一个返回值,描述新序列中的元素和原序列中元素的关系
2)map(函数, 序列1, 序列2)
函数的要求:
a. 有且只有两个参数,这两个参数分别指向后面的两个序列中的元素
b. 需要一个返回值,描述新序列中的元素和原序列中元素的关系
map(函数, 序列1, 序列2, 序列3)
…
# 1) [28, 89, 34, 78, 21] -> [8, 9, 4, 8, 1]
nums = [28, 89, 34, 78, 21]
result = map(lambda item: item % 10, nums)
print(list(result)) # [8, 9, 4, 8, 1]
# 2)
nums1 = [10, 20, 30, 40]
nums2 = [100, 200, 300, 400]
# [110, 220, 330, 440]
result = map(lambda item1, item2: item1 + item2, nums1, nums2)
print(list(result)) # [110, 220, 330, 440]
scores = [29, 89, 67, 56, 34, 90]
ages = [23, 30, 19, 24, 25, 23]
# [{'score': 29, 'age': 23}, {'score': 89, 'age': 30}, ...]
result = map(lambda item1, item2: {'score': item1, 'age': item2}, scores, ages)
print(list(result))
values = ['小明', 18, 90]
keys = ['name', 'age', 'score']
# {'name': '小明', 'age': 18, 'score': 90}
result = map(lambda item1, item2: (item1, item2), keys, values)
print(dict(result)) # {'name': '小明', 'age': 18, 'score': 90}
reduce函数 - 将序列中所有的元素通过指定的方式合并成一个数据
reduce(函数, 序列, 初始值)
函数的要求:
a. 有且只有两个参数:第一个第一次指向初始值,从第二次开始指向上一次的计算结果;第二个参数指向序列中的每个元素
b. 需要一个返回值,返回值用来描述合并规则
nums = [20, 34, 45, 10]
result = reduce(lambda x, item: x+item, nums, 0)
print(result)
nums = [2, 4, 5, 6, 3] # 2*4*5*6*3
result = reduce(lambda x, item: x * item, nums, 1)
print(result)
nums = [2, 4, 5, 6, 3] # '24563'
result = reduce(lambda x, item: x + str(item), nums, '')
print(result)
nums = [2, 4, 5, 6, 3] # '2040506030'
result = reduce(lambda x, item: f'{x}{item}0', nums, '')
print(result)
nums = [23, '45', 10, 2, '30'] # 23+45+10+2+30
result = reduce(lambda x, item: x + int(item), nums, 0)
print(result)
nums = [23, '45', 10, 2, '30'] # 23+10+2
result = reduce(lambda x, item: x + (item if type(item) == int else 0), nums, 0)
print(result)
students = [
{'name': '小明', 'age': 18, 'score': 98, 'tel': '187283822'},
{'name': '小红', 'age': 30, 'score': 99, 'tel': '187283827'},
{'name': 'Tom', 'age': 25, 'score': 87, 'tel': '187283821'},
{'name': 'Bob', 'age': 19, 'score': 65, 'tel': '187283820'}
]
result = reduce(lambda x, item: x + item['score'], students, 0) / len(students)
print(result)
2.装饰器
装饰器 = 实参高阶函数 + 返回值高阶函数 + 糖语法
装饰器是用来给函数添加功能的
2.给函数添加功能
方法一: 在需要添加功能的函数中添加代码
方法二:将需要添加的功能封装成函数
方法三:装饰器
def 装饰器名称(需要添加功能的函数):
def 添加过功能的新函数(*args, **kwargs):
反函数返回值 = 需要添加功能的函数(*args, **kwargs)
添加新的功能
return 原函数返回值(如果装饰器本身的功能和原函数返回值有关,这个地方就不一定)
return 添加过功能的新函数
def count_time(f):
def new_f(*args, **kwargs):
start = time()
result = f(*args, **kwargs)
end = time()
print(f'执行时间:{end - start}')
return result
return new_f
@count_time
def func1():
print('hello world!')
func1()
@count_time
def func22(N: int):
result = reduce(lambda x, item: x * item, range(1, N + 1), 1)
return result
# func22 = count_time(func22)
print(func22(5))
# 练习1:写一个装饰器,给函数添加功能,然后原函数结束的时候打印一个'end'
def add_end(f):
def new_f(*args, **kwargs):
result = f(*args, **kwargs)
print('end')
return result
return new_f
@add_end
def func2():
print('hello world!')
func2()
# 练习2:写一个装饰器,将函数的返回值加100。 1 -> 101
def value_add(f):
def new_f(*args, **kwargs):
result = f(*args, **kwargs)
if type(result) in (int, float, bool, complex):
return result + 100
return result
return new_f
@value_add
def func33(N: int):
result = reduce(lambda x, item: x * item, range(1, N + 1), 1)
return result
print(func33(4))
@value_add
def func2():
print('hello world!')
print(func2())
3.作业
-
为函数写一个装饰器,在函数执行之后输出 after
def add_after(f): def new_f(*args,**kwargs): result = f(*args,**kwargs) print('after') return result return new_f
-
为函数写一个装饰器,把函数的返回值 乘2再返回值
def return1(f): def new_f(*args,**kwargs): result = f(*args,**kwargs) if type(result) in (int,float,bool,complex,list): return result*2 return result return new_f
-
写一个装饰器@tag要求满足如下功能:
@tag
def render(text):
# 执行其他操作
return text
@tag
def render2():
return 'abc'
print(render('Hello')) # 打印出: <p>Hello</p>
print(render2()) # 打印出: <p>abc</p>
# 装饰器
def tag(f):
def new_f(*args,**kwargs):
result = f(*args,**kwargs)
return '<p>'+result+'</p>'
return new_f
-
求列表 nums 中绝对值最大的元素
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-234 result = max(nums,key=lambda item:item**2) print(result)
-
已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] 新字典: {'name': '张三', 'age': 18, 'sex': '女'} result = map(lambda item1,item2:(item1,item2),A,B) print(dict(result))
-
已经三个列表分别表示5个学生的姓名、学科和班号,使用map将这个三个列表拼成一个表示每个学生班级信息的的字典
names = ['小明', '小花', '小红', '老王'] nums = ['1906', '1807', '2001', '2004'] subjects = ['python', 'h5', 'java', 'python'] 结果:{'小明': 'python1906', '小花': 'h51807', '小红': 'java2001', '老王': 'python2004'} result = map(lambda item1,item2,item3:(item1,item2+item3),names,subjects,nums) print(dict(result))
-
已经一个列表message, 使用reduce计算列表中所有数字的和(用采用列表推导式和不采用列表推导式两种方法做)
message = ['你好', 20, '30', 5, 6.89, 'hello'] 结果:31.89 from functools import reduce result = reduce(lambda x,item: x+(item if type(item)in (int,float) else 0),message,0) print(result) message = [x for x in message if type(x) in (int,float)] result = reduce(lambda x,item:x+item,message,0) print(result)