Python内置函数(21)——filter
英文文档:
filter
(function, iterable)
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None
, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable)
is equivalent to the generator expression (item for item in iterable if function(item))
if function is not None
and (item for item in iterable if item)
if function is None
.
See itertools.filterfalse()
for the complementary function that returns elements of iterable for which function returns false.
说明:
1. filter函数用于过滤序列。过滤的方式则是采用传入的函数,去循环序列的元素调用,如果函数计算的结果为True则保留元素,否则将舍弃该元素。
- >>> a = list(range(1,10)) #定义序列
- >>> a
- [1, 2, 3, 4, 5, 6, 7, 8, 9]
- >>> def if_odd(x): #定义奇数判断函数
- return x%2==1
- >>> list(filter(if_odd,a)) #筛选序列中的奇数
- [1, 3, 5, 7, 9]
2. 当function参数传入None时,序列中的元素值如果为False,也会自动舍弃。
- >>> c = ['',False,'I',{}] #定义序列
- >>> c
- ['', False, 'I', {}]
- >>> list(filter(None,c)) #筛选函数为None,自动舍弃序列中的False值,空字符串、False值、空序列都是False值,所以丢弃
- ['I']
Python内置函数(21)——filter的更多相关文章
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- Python内置函数(34)——filter
英文文档: filter(function, iterable) Construct an iterator from those elements of iterable for which fun ...
- Python内置函数之filter()
filter(function,iterable)用来过滤可迭代对象 如果提供过滤条件的函数为None,则可迭代对象中为False的元素将被过滤掉. 例如: >>> a = [,,F ...
- Python内置函数(21)——tuple
英文文档: The constructor builds a tuple whose items are the same and in the same order as iterable's it ...
- [python基础知识]python内置函数map/reduce/filter
python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
随机推荐
- python正则表达式判断素数【厉害了】
参考: https://www.cnblogs.com/imjustice/p/check_prime_by_using_regular_expression.html for i in range( ...
- 记一次Hbase的行键过滤器事故问题
数据总数:746条数据 因为后面需要进行算法合成,而且spark目前对这种算法支持并不好,因此采用代码编写,所以在查询hbase的过程中采用的是java直接查询, 但是为了加快查询速度,我尽可能的使用 ...
- 在ideaUI中建立maven项目
1.新建New 注意:第一次创建maven项目需要在有网情况下进行 2.可以看见我们建立的项目了
- 思科与华为RIP配置区别
华为配置图如下: 思科配置图如下: 配置原理一样,除了配置命令有点区别:华为进入RIP的命令为:rip 1 思科进入RIP的命令为:router rip
- Helm 入门指南
Helm 为Kubernetes的软件包管理工具,Helm有两部分组成:Helm客户端.Tiller服务端,Helm三个主要部件:Chart.仓库.Release: Chart:为Kubernetes ...
- phantomjs api文档
phantomjs实现了一个无界面的webkit浏览器.虽然没有界面,但dom渲染.js运行.网络访问.canvas/svg绘制等功能都很完备,在页面抓取.页面输出.自动化测试等方面有广泛的应用. 详 ...
- [LeetCode] Advantage Shuffle 优势洗牌
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...
- 获取用户在web页面上选中的文本
window.getSelection().toString();
- Widows 和Linux 查看和操作端口方法
Windows 打开cmd1.netstat -n查看本机的使用的所有端口①.proto表示协议 有tcp和udp两种②.Local Address 表示本机的IP,后面跟的是我们使用的端口号③.Fo ...
- 三、糖醋鲤鱼(Sweet and sour carp)
糖醋鲤鱼是用鲤鱼制作的一道山东济南传统名菜,为鲁菜的代表菜品之一 ,色泽金黄,外焦内嫩,酸甜可口,香鲜味美. 菜品历史 <诗经>载:岂食其鱼,必河之鲤.<济南府志>上早有&qu ...