import time
"""
装饰器是一种专门用来给其他的函数添加功能的函数
"""
def func1():
start = time.time()
print('hello world !')
end = time.time()
print('执行时间:', end-start)
func1()
def count_time(func, *args, **kwargs):
start = time.time()
func(*args, **kwargs)
end = time.time()
print('执行时间:', end-start)
"""
装饰器 = 实参高阶函数+返回值高阶函数+糖语法
套路:
def 函数名1(参数1):
def 函数名2(*args,**kwargs):
参数1(*args,**kwargs)
实现添加功能的代码块
return 函数名2
说明:
函数名1 - 装饰器的名字(按照当前装饰器添加的功能来命名)
参数1 - 用来接收被添加功能的函数(指向没有添加功能的原函数),一般直接命名成;func
函数名2 - 表示的是在原函数的基础上添加完功能的新函数,一般直接命名成一个固定的名字:test、new_func
使用装饰器:在需要添加功能的函数的定义前加:@装饰器名
"""
def dy(func):
def test(*args, **kwargs):
func(*args, **kwargs)
print('======函数结束=======')
return test
@dy
def func2(x, y):
print(x+y)
@dy
def func3():
print('hello, 装饰器')
func2(20, 30)
func3()
def binary(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
if type(result) != int:
return result
return bin(result)
return new_func
@binary
def func7(x, y):
return x+y
print(func7(20, 30))
print(func7(1.23, 2.34))
print(func7('abc', '123'))
"""
a.普通用法:max(序列) - 求序列中元素的最大值(序列中元素的类型必须一致,而且元素本身必须支持比较运算)
b.实参高阶函数:
max(序列,key=函数) - 通过函数来定制求最大值的方式
序列 - 需要获取某种最大值对应的序列
函数 - 有且只有一个参数(指向/代表序列中每一个元素);有一个返回值(求最大值的时候的比较对象)
"""
students = [
{'name': 'stu1', 'age': 20, 'score': 67, 'sex': '男'},
{'name': 'stu2', 'age': 18, 'score': 82, 'sex': '女'},
{'name': 'stu3', 'age': 19, 'score': 54, 'sex': '女'},
{'name': 'stu4', 'age': 22, 'score': 77, 'sex': '男'},
{'name': 'stu5', 'age': 21, 'score': 56, 'sex': '男'},
{'name': 'stu6', 'age': 18, 'score': 78, 'sex': '男'},
{'name': 'stu7', 'age': 16, 'score': 100, 'sex': '女'}
]
result = max(students, key=lambda item: item['age'])
print('年龄最大的学生:', result)
result = min(students, key=lambda item: item['score'])
print('成绩最低的学生:', result)
result = max([x for x in students if x['sex'] == '女'], key=lambda item: item['age'])
print('女生年龄最大的:', result)
"""
用法一:sorted(序列) - 将序列中的元素按照元素的大小从小到大排序
用法二:
sorted(序列,key=函数)
函数 - 有且只有一个参数(指向/代表序列中的每个元素);有一个返回值(比较返回值的大小进行排序的;返回值就是排序标准)
"""
datas = [23, '78', 56, '34', '102', 79]
result = sorted(datas, key=lambda item: int(item))
print(result)
result = sorted(int(x) for x in datas)
print(result)
"""
用法一:
map(函数,序列) - 通过原序列中的元素进行指定的变换后产生一个新的序列(返回的是map对象,本质是序列)
a.函数 - 有且只有一个参数(指向/代表后面的序列中的元素);有一个返回值(新序列中的元素)
用法二:
map(函数,序列1,序列2) - 产生一个新的序列,新序列中的元素是序列1和序列2中元素通过指定的变换产生的
a.函数 - 有且只有两个参数(这两个参数分别指向序列1中的元素和序列2中的元素);
有一个返回值(新序列中的元素)
"""
nums = [1, 2, 3, 4]
result = list(map(lambda item: item ** 2, nums))
print(result)
seq1 = [1, 2, 3, 4]
seq2 = (5, 6, 7, 8)
result = list(map(lambda item1, item2: item1 + item2, seq1, seq2))
print(result)
students = [
{'name': 'stu1', 'age': 20, 'score': 67, 'sex': '男'},
{'name': 'stu2', 'age': 18, 'score': 82, 'sex': '女'},
{'name': 'stu3', 'age': 19, 'score': 54, 'sex': '女'},
{'name': 'stu4', 'age': 22, 'score': 77, 'sex': '男'},
{'name': 'stu5', 'age': 21, 'score': 56, 'sex': '男'},
{'name': 'stu6', 'age': 18, 'score': 78, 'sex': '男'},
{'name': 'stu7', 'age': 16, 'score': 100, 'sex': '女'}
]
tels = ['110', '120', '114', '119', '12306', '10086', '10000']
result = list(map(lambda x, y: eval(f'{str(x)[:-1]},"tel":"{y}"}}'), students, tels))
print('方法二:', result)
"""
使用之前需要从functools模块导入
用法一(了解):
reduce(函数,序列) - 将序列中的元素通过指定的规则合并成一个数据
a.函数 - 有且只有两个参数,第一个参数第一次调用的时候指向序列中的第一个元素,从第二次开始都是指向上一次运算的结果;第二个参数指向除了第一个元素以外的所有元素;
返回值是每一次合并的结果(用来定制合并规则的)
用法二(掌握):
reduce(函数,序列,初始值)
a.函数:
第一个参数:第一次指向初始值;从第二次开始参数指向上次合并的结果
第二个参数:指向序列中所有的元素
返回值:每一次合并的结果(用来定制合并规则的)
"""
from functools import reduce
nums = [10, 11, 12, 13, 14]
def func1(x, y):
return x + y
result = reduce(func1, nums)
print(result)
nums1 = [29, 38, 90, 34, 67]
result = reduce(lambda x, y: x + y % 10, nums1, 0)
print(result)
result = reduce(lambda x, y: str(x) + str(y % 10), nums1, '')
print(f'{result}')
result = reduce(lambda x, y: x + y['score'], students, 0)
print(result)