Python内置函数(34)——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 iffunction(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 functionreturns 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内置函数(34)——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内置函数(21)——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内置函数(34)——map
英文文档: map(function, iterable, ...) Return an iterator that applies function to every item of iterabl ...
- Python内置函数(34)——isinstance
英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...
- [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内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
随机推荐
- 2D变形transform的translate和rotate
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Cloudera Manager及CDH最新版本安装全程记录
大家都知道,Apache Hadoop的配置很繁琐,而且很零散,为此Cloudera公司提供了Clouder Manager工具,而且还封装了Apache Hadoop,flume,spark,hiv ...
- 基于docer搭建私有gitlab服务器
今天闲着无聊,于是乎想用最近很流行的docker容器搭建一个自己的gitlab的服务器,关于docker和gitlab就不多介绍了,网上查了很多资料,貌似没有一个统一的方法,很乱很杂,而且很容易误导人 ...
- IPFS如何挖矿<Filecoin系统>?(一)
本来这篇文章应该晚一点写, 但是这几天一直有朋友在公众号留言, 迫切的想知道IPFS到底如何挖矿, 所以就提前写一篇关于IPFS挖矿的文章. 本文暂不涉及具体的技术细节, 只做大概的介绍. 首先, 好 ...
- 使用guava实现找回密码的tokenCache以及LRU算法
源码包的简单说明: com.google.common.annotations:普通注解类型. com.google.common.base:基本工具类库和接口. com.google.common. ...
- CXF-02: 使用CXF处理JavaBean式的复合类型和List集合类型
Cat.java: package com.war3.ws.domain; public class Cat { private Integer id; private String name; pr ...
- spring boot多环境配置 直接上代码
spring: profiles: active: test jackson: date-format: yyyy-MM-dd HH:mm:ss datasource: dri ...
- 关于html表单的disabled属性的设置问题
首先,我的看法是无论disable的值是否有值,只要设置了disabled属性的表单,无论是否有值,无论值为什么,都会被禁用. 来看下面例子: 在一个群里有人说因为直接写在表单属性上是字符串,因为 ...
- 【Django】模型层说明
[Django模型层] 之前大概介绍Django的文章居然写了两篇..这篇是重点关注了Django的模型层来进行学习. ■ 模型定义 众所周知,Django中的模型定义就是定义一个类,其基本结构是这样 ...
- [git 实践篇]如何创建公钥
如何创建公钥 首先启动一个Git Bash窗口(非Windows用户直接打开终端) 执行: cd ~/.ssh 如果返回"- No such file or directory", ...