由于 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]
由于 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]