python函数之filter函数
来源:互联网 发布:淘宝极有家装修日记 编辑:程序博客网 时间:2024/06/05 05:36
python函数之filter函数
实例环境
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:40:30) [MSC
v.1500 64 bit ( AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> help(filter)Help on built-in function filter in module __builtin__:filter(...) filter(function or None, sequence) -> list, tuple, or string Return 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.
filter参数的参数:第一个是函数对象或者None,第二个是序(sequence),如果第一个参数 是一个函数对象,则将第二个参数(可迭代的序列)作为函数参数传入函数,函数执行并把返回ture
的之筛选出来,并成一个同传入序列同类型的序列,如果第一个参数为None,则将第二个参数 里面为Ture的值筛选出
实例一:
当第一个参数为None时,筛选出第二个参数列表中元素符合True的值,并且以同类型的序列打包返回
>>> filter(None,[1,2,0,False,True])[1, 2, True]
返回的值为列表类型
>>> t = filter(None,[1,2,0,False,True])>>> type(t)<type 'list'>>>> t[1, 2, True]
实例二:
将filter函数直接以迭代器的形式应用
>>> for i in filter(None,[1,2,0,False,True]):... print i...12True
实例三:
当第一个参数为一个函数时
>>> def odd(x):... return x % 2...>>> t2 = filter(odd, range(10))>>> t2[1, 3, 5, 7, 9]>>
实例四:
filter函数与lambda函数表达式的结合应用
>>> t3 = filter(lambda x : x % 2,range(10))>>> t3[1, 3, 5, 7, 9]
0 0
- python函数之filter函数
- python之filter()函数
- Python学习之filter函数
- Python函数式编程之filter
- Python之map(),reduce(),filter()函数
- python 常用函数 filter
- python小点心--filter函数
- Python filter()函数
- python filter函数
- python中filter()函数
- python中的filter()函数
- python-内置函数filter()
- python中的filter函数
- python filter函数
- python中filter()函数
- Python 内置函数 filter()
- python 中filter()函数
- python中filter()函数
- window下python软件包安装
- Mysql-MHA高可用实验测试-主从复制搭建
- Easy 14 Plus One(66)
- MATLAB求和函数sum/cumsum
- Libgdx使用ShapeRenderer画图形,设置抗锯齿
- python函数之filter函数
- 【Linux】查看当前系统的负载情况
- H5 AUI 使用总结
- Canvas与Drawable绘制图形
- BOM 对象
- 构造函数,拷贝构造函数,析构函数
- 走进JEDEC,解读DDR(下)
- Python numpy基础知识
- Android开发框架XUtils简介