代码点读机 / Python / filter

filter Python · 内置函数

按条件筛选可迭代对象里的元素,只保留让条件为真的那些。

怎么用

filter(判断函数, 可迭代对象)

小例子

print(list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4])))

运行结果

[2, 4]

官方文档:https://docs.python.org/zh-cn/3/library/functions.html#filter

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条