2021.03.16高阶函数

2021.03.16

总结

函数的本质

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))

# 2.变量的常用操作
# 1)
a = 10
x1 = a
print(x1, x1 + 10)

x2 = func1
x2()

# 2)
list1 = [a, 100]
print(list1, list1[0] * 10)

list1 = [func1, 100]
print(list1, list1[0]())

# 3)
a = 100
print(a)

func1 = 100
print(func1)


# 4)
def func2(x):
    print(x)


def func1():        # func1 是变量
    print('函数')


a = 10
func2(a)
func2(func1)


# 5)
def func3():
    aa = 10
    return aa


def func4():
    def fn():
        print('小函数')
    return fn


func4()()     # fn()


def test(x):
    x(10)


test(func2)

高阶函数

实参高阶函数 - 如果一个函数的参数是函数,那么这个函数就是实参高阶函数 (掌握怎么使用系统提供的实参高阶函数)

返回值高阶函数 - 如果一个函数的返回值是函数,那么这个函数就是返回值高阶函数(会写装饰器)

实参高阶函数

常用:max、min、sorted、map、reduce

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

练习:求列表中各个位上和最大的元素

nums = [28, 607, 65, 19, 88]

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)

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函数

from functools import reduce

将序列中所有的元素通过指定的方式合并成一个数据

reduce(函数, 序列, 初始值)
函数的要求:
a. 有且只有两个参数:第一个第一次指向初始值,从第二次开始指向上一次的计算结果;第二个参数指向序列中的每个元素
b. 需要一个返回值,返回值用来描述合并规则

nums = [20, 34, 45, 10]
result = reduce(lambda x, item: x+item, nums, 0)
print(result)

reduce内部执行过程:
循环4次:
第1次
x = 0, item = 20, x = x + item = 0 + 20 = 20

第2次
x = 20, item = 34, x = x + item = 20 + 34 = 54

第3次
x = 54, item = 45, x = x + item = 54 + 45 = 99

第4次
x = 99, item = 10, x = x + item = 99 + 10 = 109

return x

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)

装饰器

装饰器 = 实参高阶函数 + 返回值高阶函数 + 糖语法

装饰器是用来给函数添加功能的

给函数添加功能

方法一: 在需要添加功能的函数中添加代码

问题:如果要给不同的函数添加相同的功能,添加的功能的对应的代码需要重复写

def func1():
    start = time()    # time()  - 获取当前时间
    print('hello world!')
    end = time()
    print('执行时间:', end - start)


def func2(N: int):
    start = time()
    result = reduce(lambda x, item: x * item, range(1, N+1), 1)
    print(result)
    end = time()
    print('执行时间:', end - start)


func2(8)

方法二:将需要添加的功能封装成函数

调用函数的时候需要调用的时候新增功能,而不是添加过功能的原函数 - 感觉

def count_time(fn, *args, **kwargs):
    # fn = func22
    start = time()
    fn(*args, **kwargs)    # 执行函数   func22()
    end = time()
    print('执行时间:', end - start)


def func11():
    print('hello world!')


def func22(N: int):
    result = reduce(lambda x, item: x * item, range(1, N + 1), 1)
    print(result)


def func33(a, b, c):
    print(f'a:{a}, b:{b}, c:{c}')


count_time(func11)

count_time(func22, 4)

# func33(10, 20, 30)
count_time(func33, 10, 20, c=30)

补充*的用法: 打包、解包

def f1(*nums):
    print(nums)


f1(2, 3, 4)    # nums = (2, 3, 4)

x, *y = 10, 20, 30, 40

t = (1, 11, 22)
print(t)    # (1, 11, 22)


print(*t)   # print(1, 11, 22)  -> 1 11 22

d = {'a': 10, 'b': 20}   # **d  -> a=10, b=20
d2 = {'end': ';', 'sep': ','}
print('abc', 123, **d2)    # print('abc', 123, end=';', sep=',')


def f2(f, *n, **kwargs):
    # f = f3, n = (10, 20, 30)
    f(*n, **kwargs)    # f3(10, 20, 30)  f3(x=10, y=20, z=30)


def f3(x, y, z):
    print('函数')


f2(f3, x=10, y=20, z=30)

方法三:装饰器

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())

作业

1.为函数写一个装饰器,在函数执行之后输出 after

def add_end(f):
    def new_f(*args,**kwargs):
        result=f(*args,**kwargs)
        print('after')
        return result
    return new_f


@add_end
def func1():
    pass

func1()

2.为函数写一个装饰器,把函数的返回值 乘2再返回值

def value_add(f):
    def new_f(*args, **kwargs):
        result = f(*args, **kwargs)
        if type(result) in (int, float, bool, complex):
            return result * 2
        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))

3.写一个装饰器@tag要求满足如下功能:

@tag
def render(text):
# 执行其他操作
return text

@tag
def render2():
return ‘abc’

print(render(‘Hello’)) # 打印出:

Hello


print(render2()) # 打印出:

abc

def tag():
    def new_tag(*args,**kwargs):
        result=f(*args,**kwargs)
        return '<p> '+result +'</p>'
    return new_tag

4.求列表 nums 中绝对值最大的元素

例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-234

nums = [-23, 100, 89, -56, -234, 123]
result=max(nums,key=lambda item:item**2)
print(result)

5.已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value

A = [‘name’, ‘age’, ‘sex’]
B = [‘张三’, 18, ‘女’]
新字典: {‘name’: ‘张三’, ‘age’: 18, ‘sex’: ‘女’}

A = ['name', 'age', 'sex']
B = ['张三', 18, '女']

result=map(lambda item1,item2:(item1,item2),A,B)
print(dict(result))

6.已经三个列表分别表示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=map(lambda item1,item2,item3:(item1,item2+item3),names,nums,subjects)
print(dict(result))

7.已经一个列表message, 使用reduce计算列表中所有数字的和(用采用列表推导式和不采用列表推导式两种方法做)

message = [‘你好’, 20, ‘30’, 5, 6.89, ‘hello’]
结果:31.89

from functools import reduce
message = ['你好', 20, '30', 5, 6.89, 'hello']
result=reduce(lambda x,item:x+item if not type(item)==str else x+0,message,0)
print(result)
def result(list1):
    s=0
    for x in list1:
        if not type(x)==str:
            s+=x
        else:
            s+=0
        print(s)

result(['你好', 20, '30', 5, 6.89, 'hello'])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值