登录
登录 注册新账号
注册
已有账号登录

不多说,直接贴代码,代码非常短

print(list(filter(None,[1,2,3,0,0,-1,4,-6,5])))

刚开始我自己认为输出应该是

[1, 2, 3, 4, 5]

但是实际输出却是

[1, 2, 3, -1, 4, -6, 5]

我的疑惑就是为什么这里的-1,-6也会输出,filter帮助文档里明明写的
Return an iterator yielding those items of iterable for which function(item) is true.

If function is None, return the items that are true

这里的-1,-6为什么会是True?

1 个回答

python 里面,所有非 0 值都是 True

撰写答案