由于 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]
关于python - 为什么不过滤(不是无,可迭代)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38023300/