python中map、reduce、filter、自定义排序函数、装饰器函数和偏函数
来源:互联网 发布:淘宝评论上传视频 编辑:程序博客网 时间:2024/06/07 06:23
import functoolsfrom functools import reduce# map()是python内置高阶函数,功能是接收一个函数和一个list# 把函数f依次作用在list的每一个元素上,得到一个新的list并返回。# 例1. 把list的每个元素都做平方并返回def f(x): return x * xprint(list(map(f, [1, 2, 3, 4, 5])))print(list(map(lambda x: x*x, [1, 2, 3, 4, 5]))) # 利用匿名函数lambda# 注意:由于list可以包含任意类型的元素,故map()不仅可以处理只包含数值的list# 还可以处理包含任意类型的list,只要传入的函数f能处理该数据类型# reduce()函数也是python内置高阶函数,接收的参数和map()类似,一个函数f和一个list,但行为有所不同# reduce传入的f必须接收两个参数,对list的每个元素反复调用函数f,并返回最终结果值# 例1. 对list中的所有元素求和def f(x, y): return x + yreduce(f, [1, 2, 3, 4, 5])# reduce还可以接收第三个可选参数,作为计算初始值# 例2. 对list中所有元素求和,并加上100def f(x, y): return x + yreduce(f, [1, 2, 3, 4, 5], 100)# filter函数是python内置的另一有用的高阶函数,它接收一个函数f和一个list,函数f对每个元素进行判断# 返回True或False,filter根据判断结果自动给过滤掉不符合条件的元素,返回又符合条件的元素组成的新list# 例1. 从list中删除偶数,保留奇数def isOdd(x): return x % 2 == 1filter(isOdd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])# 例2. 删除None或空字符串def isNotEmpty(s): return s and len(s.strip()) > 0filter(isNotEmpty, ['test', None, '', ' '])# sort()也是高阶函数,可以接收一个比较函数来实现自定义排序,比较函数的定义是:传入两个带比较的元素x,y# 如果x<y则返回-1,x>y则返回1,x=y则返回0# 例1. 对list进行排序sorted([3, 6, 2, 7, 38, 9])# 例2. 如果我们要实现倒序排序,只需自己编写一个reversedCmp函数def reversedCmp(x, y): if x > y: return -1 if x < y: return 1 return 0# sorted([5,3,6,4,1,7],reversedCmp) #python3已经不支持了print(sorted(['bus','amount','Zero','Ceiling'], key=None))# 装饰器def fPrint(f): #装饰器函数 def fn(x): print('call '+f.__name__+"()") return f(x) return fndef f1(x): return x * x# f1 = fPrint(f1)# 等价于@fPrintdef f1(x): return x*xprint(f1.__name__) # 可以看到函数名已经不是'f1',而是@fPrint内部定义的'fn'# 所以对于那些依赖函数名的代码就会失效,而且decorator还改变了函数的__doc__等其他属性# 如果要让调用者看不出一个函数经过了@decorator的"改造",就需要把原函数的一些属性复制到新函数中def fPrint(f): #装饰器函数 @functools.wraps(f) def fn(x): print('call '+f.__name__+"()") return f(x) return fn@fPrintdef f1(x): return x*xprint(f1.__name__) # 这样函数名就变为'f1'了print(f1(2))int('1010101') # 默认是十进制int2 = functools.partial(int, base=2) # 利用偏函数设置参数为二进制的新函数int2('1010101')
阅读全文
0 0
- python中map、reduce、filter、自定义排序函数、装饰器函数和偏函数
- Python 函数式编程(高阶函数、把函数作为参数、map()函数、reduce()函数、filter()函数、自定义排序函数、函数返回函数、闭包、匿名函数、装饰器decorator)
- Python函数式编程(高阶函数 <map/reduce filter sorted> 返回函数 匿名函数 装饰器 偏函数)
- python中map、filter、reduce 函数使用方法
- Python中filter、map、reduce函数
- python中map,filter和reduce函数区别
- Python 中 map,reduce,和filter 内置函数的方法
- python-map()函数,reduce()函数,filter()函数
- python map函数,Filter函数,Reduce函数
- python map函数,filter,reduce
- python map, reduce, filter 函数
- Python函数式编程(高阶函数、map()、reduce()、filter()、sorted()、lambda、decorator装饰器)
- Python3.4中filter函数,map函数和reduce函数
- Python3.4中filter函数,map函数和reduce函数
- Python3.4中filter函数,map函数和reduce函数
- Map Filter和Reduce函数
- python 高阶函数 map reduce filter sorted 闭包 装饰器
- map、filter、reduce函数
- 64位Windows10下tensorflow的安装
- 关于官方教程Space Shooter子弹生成部分提示MissingReferenceException的解决办法
- (CURD)
- 012
- Linux查看物理CPU个数、核数、逻辑CPU个数
- python中map、reduce、filter、自定义排序函数、装饰器函数和偏函数
- springMVC配置了编码过滤器中文依旧乱码
- sqlserver2016 收缩日志
- Elasticsearch-2.4.3的单节点安装
- 成为一名 AI 工程师永远都不晚
- 013
- Day16
- 什么是云数据库 Redis 版
- HX711称重传感器