2021-03-16

高阶函数

function is a variable

1.函数的本质

Python中定义函数其实就是再定义一个类型是function的变量;
变量能做的函数都可以做

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

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

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

2.高阶函数
实参高阶函数 - 如果一个函数的参数是函数,那么这个函数就是实参高阶函数(掌握怎么使用系统提供的实参高阶函数)
返回值高阶函数 - 如果一个函数的返回值是函数,那么这个函数就是返回值高阶函数(会写装饰器)

Arguments to higher_oder functions

1.常用的实参高阶函数 :max、min、sorted、map、reduce

2.max 和 min
“”"
max(序列,key = 函数)
sorted(序列, Key = 函数)
列表.sort(key = 函数)
函数的要求:
a.有 且只有一个参数,这个参数指向的前面序列中的每个元素
b.需要一个返回值,返回值决定求最大值的时候比较的对象

“”"
2.map函数 - 将原序列中元素按照指定的标准进行变换,产生一个新的序列
“”"
1)map(函数,序列)
函数的要求:
a.有且只有一个参数,这个参数指向的是后面这个序列每个元素
b.需要一个返回值,描述新序列中的元素和原序列中元素的关系

map(函数,序列1,序列2)
map(函数,序列1,序列2,序列3)
“”"
1)一个序列
nums = [28, 89, 34, 78, 21] # 将元素个位数取出来组成新的列表
result=map(lambda item:item % 10, nums)
print(list(result))
2)多个序列
“”"
map(函数,序列1,序列2)
“”"
a.有且只有一个参数,这个参数指向的是后面两个序列每个元素
b.需要一个返回值,描述新序列中的元素和原序列中元素的关系

3.reduce函数 - 将序列中所有的元素通过指定的方式合并成一个数据
“”"
reduce(函数,序列,初始值)
函数的要求:
a.有且只有两个参数:第一个参数第一次指向初始值,从第二次开始指向上一次的计算结果;第二个参数指向序列中的每个元素
b.需要一个返回值,返回值用来描述合并规则

“”"

decorator

1.什么是装饰器
装饰器 = 实参高阶函数 + 返回值高阶函数 + 糖语法
装饰器是用来给函数添加功能的

2.给函数添加功能
方法一:在需要添加功能的函数中添加代码
问题:如果要给不同的函数添加相同的功能,添加的功能的对应的代码需要重复写

方法二:将需要添加的功能封装成函数
调用函数的时候需要调用的是新增功能,而不是添加过功能的原函数

给需要参数的函数添加功能
def count_time(fn, *args, **kwargs):
start = time()
fn(*args, **kwargs) # 执行函数
end = time()
print(‘执行时间:’, end - start)

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

def f1(*nums):
print(nums)

方法三:装饰器
“”"
def 装饰器名称(需要添加功能的函数):
def 添加过功能的新函数(*args, **kwargs):
反函数返回值=需要添加功能的函数((*args, **kwargs))
添加新的功能
return 原函数返回值(如果装饰器本身的功能和原函数返回值有关,这个地方就不一定)
return 添加过功能的新函数
“”"

def count_time(f):
def new_f(*args, **kwargs):
start = time()
f(*args, **kwargs)
end = time()
print(f’执行时间:{end - start}’)
return result
return new_f

  1. 为函数写一个装饰器,在函数执行之后输出 after
def nums_(f):
    def new_f(*args, **kwargs):
        result=f(*args, **kwargs)
        print('after')
        return result
    return new_f
  1. 为函数写一个装饰器,把函数的返回值 乘2再返回值
def nums_(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
  1. 写一个装饰器@tag要求满足如下功能:
@tag
def render(text):
    # 执行其他操作
    return text

@tag
def render2():
    return 'abc'

print(render('Hello'))   # 打印出: <p>Hello</p>
print(render2())     # 打印出: <p>abc</p>

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

    例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-234
    
    nums = [-23, 100, 89, -56, -234, 123]
    result = max(nums,key = lambda item:item ** 2)
    
  2. 已经两个列表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)
    
  3. 已经三个列表分别表示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))
    
    
  4. 已经一个列表message, 使用reduce计算列表中所有数字的和(用采用列表推导式和不采用列表推导式两种方法做)

    message = ['你好', 20, '30', 5, 6.89, 'hello']
    结果:31.89
    
    方法一:
    message = ['你好', 20, '30', 5, 6.89, 'hello']
    result =reduce(lambda x, item: x+(item if type(item) == float or type(item ) ==  int else 0), message,0)
    print(result)
    方法2:
    message = ['你好', 20, '30', 5, 6.89, 'hello']
    count = 0
    for x in message:
        if type(x) == int or type(x) == float:
            count += x
    print(count)
    
    
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2021-03-26 20:54:33,596 - Model - INFO - Epoch 1 (1/200): 2021-03-26 20:57:40,380 - Model - INFO - Train Instance Accuracy: 0.571037 2021-03-26 20:58:16,623 - Model - INFO - Test Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Best Instance Accuracy: 0.718528, Class Accuracy: 0.627357 2021-03-26 20:58:16,623 - Model - INFO - Save model... 2021-03-26 20:58:16,623 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 20:58:16,698 - Model - INFO - Epoch 2 (2/200): 2021-03-26 21:01:26,685 - Model - INFO - Train Instance Accuracy: 0.727947 2021-03-26 21:02:03,642 - Model - INFO - Test Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Best Instance Accuracy: 0.790858, Class Accuracy: 0.702316 2021-03-26 21:02:03,642 - Model - INFO - Save model... 2021-03-26 21:02:03,643 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 2021-03-26 21:02:03,746 - Model - INFO - Epoch 3 (3/200): 2021-03-26 21:05:15,349 - Model - INFO - Train Instance Accuracy: 0.781606 2021-03-26 21:05:51,538 - Model - INFO - Test Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,538 - Model - INFO - Best Instance Accuracy: 0.803641, Class Accuracy: 0.738575 2021-03-26 21:05:51,539 - Model - INFO - Save model... 2021-03-26 21:05:51,539 - Model - INFO - Saving at log/classification/pointnet2_msg_normals/checkpoints/best_model.pth 我有类似于这样的一段txt文件,请你帮我写一段代码来可视化这些训练结果
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值