Python中filter与lambda的结合使用
来源:互联网 发布:f1 2015知乎 编辑:程序博客网 时间:2024/06/10 07:30
filter是Python的内置方法。官方定义是:
filter(function or None, sequence) -> list, tuple, or stringReturn those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.
第一个参数为None
的情形:
filter(None, '101') # '101'filter(None, [True,False]) #[True]filter(None, [True, 0, 1, -1]) #[True, 1, -1]filter(None, (True, 1, 0, -1, False)) #(True, 1, -1)
第一个参数为function的情形,如果function(item)为True,则满足过滤条件。此时的lambda函数的形式是: lambda x: expression(x)
。注意到,:
左边只能有一个元素x
,:
右边为一个关于x
的表达式,且这个表达式的值要么是True, 要么是False.
filter(lambda x: x, [-1, 0, 1]) #[-1, 1]filter(lambda x: not x, [-1, 0, 1]) #[0]def f(x): return True if x == 1 else Falsefilter(lambda x: f(x), [-1, 0, 1]) #[1]
阅读全文
0 0
- Python中filter与lambda的结合使用
- Python中map与lambda的结合使用
- Python中reduce与lambda的结合使用
- Python中filter、lambda、map、reduce的使用
- C++ 的lambda 与template结合使用
- C++ priority_queue 与 lambda的结合使用
- Python中lambda的使用
- python中lambda的使用
- Python中 filter | map | reduce | lambda的用法
- Python中filter、map、reduce、lambda 的用法
- [Python]中filter、map、reduce、lambda的用法
- Python filter()、map()、reduce()与lambda
- python中lambda的另类使用
- Python中lambda匿名函数的使用
- python中lambda表达式的使用
- python中匿名函数lambda的使用
- Python的filter, map, reduce, lambda
- Python 的lambda 以及 filter , map , reduce
- 设计模式(一)---单例模式
- 关于Android Studio 导入Eclipse 项目问题
- POJ2387-Til the Cows Come Home-最短路
- Linux下通过源码编译安装程序
- sad satd ssd Hadamard变换
- Python中filter与lambda的结合使用
- Atitit 战略之道 attilax著 v4 r88
- Java基础001
- 网易编程题--数字翻转
- 动态规划
- 二叉树问题---二叉树节点间的最大距离
- h5中视频插入标签 video
- unity 协程笔记
- Standby RAC遭遇ORA-1157,1111,1110导致实例crash处理