python高级特性和高阶函数
2017-03-10 22:08
537 查看
python高级特性
1、集合的推导式列表推导式,使用一句表达式构造一个新列表,可包含过滤、转换等操作。
语法:[exp for item in collection if codition]
if codition - 可选
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310213953826-1551313187.png)
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310214003092-609240154.png)
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310214228061-2107926483.png)
字典推导式,使用一句表达式构造一个新列表,可包含过滤、转换等操作。
语法:{key_exp:value_exp for item in collection if codition}
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310214239326-1685063682.png)
集合推导式
语法:{exp for item in collection if codition}
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310214452561-1602069243.png)
嵌套列表推导式
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310214934279-2136230642.png)
2、多函数模式
函数列表,python中一切皆对象。
# 处理字符串 str_lst = ['$1.123', ' $1123.454', '$899.12312'] def remove_space(str): """ remove space """ str_no_space = str.replace(' ', '') return str_no_space def remove_dollar(str): """ remove $ """ if '$' in str: return str.replace('$', '') else: return str def clean_str_lst(str_lst, operations): """ clean string list """ result = [] for item in str_lst: for op in operations: item = op(item) result.append(item) return result clean_operations = [remove_space, remove_dollar] result = clean_str_lst(str_lst, clean_operations) print result
执行结果:['1.123', '1123.454', '899.12312']
3、匿名函数lambda
没有函数名
单条语句组成
语句执行的结果就是返回值
可用作sort的key函数
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310215536326-534009180.png)
python高阶函数
1、函数式编程函数本身可以赋值给变量,赋值后变量为函数;
允许将函数本身作为参数传入另一个函数;
允许返回一个函数。
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310215833467-724829973.png)
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310215844701-1733437154.png)
2、map/reduce函数
map(fun, lst),将传入的函数变量func作用到lst变量的每个元素中,并将结果组成新的列表返回
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310220404670-70609836.png)
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310220447467-869475960.png)
reduce(func(x,y),lst),其中func必须有两个参数。每次func计算的结果继续和序列的下一个元素做累积计算。
lst = [a1, a2 ,a3, ......, an]
reduce(func(x,y), lst) = func(func(func(a1, a2), a3), ......, an)
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310220423998-1221915239.png)
3、filter函数
筛选序列
filter(func, lst),将func作用于lst的每个元素,然后根据返回值是True或False判断是保留还是丢弃该元素。
![](https://images2015.cnblogs.com/blog/830561/201703/830561-20170310220736826-382252805.png)
相关文章推荐
- python基础知识5(集合,高级特性,高阶函数)
- python高级特性与高阶函数
- Python五(集合&冻集&高级特性&高阶函数)
- Python自学笔记之高级特性1——切片和迭代
- Python高级特性:类构造与析构
- Python高级特性-列表解析
- Python自学笔记之高级特性2——列表生成式
- Python高级特性
- 三、python高级特性(切片、迭代、列表生成器、生成器)
- Python高级特性:类属性
- Python高级特性:私有属性
- Python_高级特性
- Python的高级特性
- Python高级特性(3): Classes和Metaclasses(转)
- python高级特性
- Python高级特性之:List Comprehensions、Generator、Dictionary and set comprehensions
- Python的高级特性之切片、迭代、列表生成式、生成器
- Python自学笔记之高级特性——生成器
- Python高级特性:利用类构造及析构原理实现单实例模式