Skip to main content
 首页 » 编程设计

python之为什么不过滤(不是无,可迭代)

2025年05月04日13dflying

由于 filter() 的定义是返回一个元素列表,其中函数返回 True,所以 filter(not None, iterable ) 对过滤到仅包含不是 NoneType 的项目的列表更有意义吗?

请您参考如下方法:

对于 python3 你不需要使用 lambda,None.__ne__ 只会过滤 None:

In [1]: list(filter(None.__ne__, [None, 0, 1, 2])) 
Out[1]: [0, 1, 2]