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

标签 python python-3.x

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

相关文章:

python - 避免程序在 I/O 错误时退出

python - python多线程处理打开多个webdrivers时报错

python - Pandas 数据框 : how to select rows where one column-value is like 'values in a list'

python - 具有多个特征的 train_test_split

python-3.x - Discord Bot 不响应命令

python - 创建排列矩阵

python - 通过 Python 连接远程 MySQL 数据库

python - 使事件以设定速率发生的最佳方式是什么?使用 time.sleep() 非常不可靠。事情可以被锁定到某种计时器吗?

python - Tkinter 单选按钮未传递值

python - Selenium 中的下拉菜单 - Python 3