https://onitroad.com 更多教程

示例:过滤数组并返回一个新数组,其中只有等于或者大于 33 的值

marks = [50, 26, 67, 18, 24, 86]
def myFunc(x):
  if x < 33:
    return False
  else:
    return True
passing_marks = filter(myFunc, marks)
for x in passing_marks:
  print(x)

Python filter() 函数语法

filter(function, iterable)
Python filter() 函数

在 python 中 filter() 是一个内置函数,它返回一个迭代器,其中通过函数过滤项目以测试项目是否被接受。

日期:2020-09-17 00:10:31 来源:oir作者:oir