zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python3 filter函数使用

Python3 函数 filter 使用
2023-09-11 14:14:44 时间

作用

filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判断,然后返回 True 或 False,最后将返回 True 的元素放到新列表中

注意

Python2.7 返回列表,Python3.x 返回迭代器对象。

语法

filter(function, iterable)

参数

  • function -- 判断函数。
  • iterable -- 可迭代对象。

返回值

返回列表。

实例

过滤出列表中的所有奇数

#!/usr/bin/python3
 
def is_odd(n):
    return n % 2 == 1
 
tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
newlist = list(tmplist)   # python2无需这一步直接就是列表了
print(newlist)

结果

[1, 3, 5, 7, 9]

过滤出1~100中平方根是整数的数

#!/usr/bin/python3
 
import math
def is_sqr(x):
    return math.sqrt(x) % 1 == 0
 
tmplist = filter(is_sqr, range(1, 101))
newlist = list(tmplist)   # python2无需这一步直接就是列表了
print(newlist)

结果

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]