那些Python方法---filter()

来源:互联网 发布:苹果app移动数据下载 编辑:程序博客网 时间:2024/06/03 17:04

作用:第一个参数为一个函数对象,其他参数为序列,将序列的每个元素通过函数进行加工筛选,符合的留下、返回True,不符合的剔除、返回False

适用:somewhere you want

示例:

>>> i = [1, 2, 3, 4]>>> j = [5, 6, 7, 8]>>> def a(x):...     if x > 5:...         return x...     else:...         return 0...>>> list(filter(a, i))[]>>> list(filter(a, j))[6, 7, 8]>>>

>>> list(filter((lambda x:x >2), j))[5, 6, 7, 8]

注:

函数接收一个参数

0 0