zoukankan      html  css  js  c++  java
  • Python内置函数(34)——filter

    英文文档:

    filter(functioniterable)

    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']
  • 相关阅读:
    大数据下的质量体系建设
    快速打造属于你的接口自动化测试框架
    测试环境问题排查的那些事儿
    100个任务,用多机实现
    shell 在一个文件中查找数字
    shell中的awk使用
    shell怎么实现多进程
    删除字符串S1中的子串S2
    C++11的新特性
    C++里面普通指针怎么转换成智能指针
  • 原文地址:https://www.cnblogs.com/lincappu/p/8144917.html
Copyright © 2011-2022 走看看