如何从列表中删除None值而不删除0值?

  • 回答 (2)
  • 关注 (0)
  • 查看 (9)

这是我的函数值:

L = [0, 23, 234, 89, None, 0, 35, 9]

当运行这个:

L = filter(None, L)

得到了这个结果

[23, 234, 89, 35, 9]

需要的详细代码是:

[0, 23, 234, 89, 0, 35, 9]

如何在不删除0值的情况下从列表中删除NONE值?

爹说的都是真理爹说的都是真理提问于
好好看解密右转天后不带记忆与方向生活回答于
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]

可以适应的方法filter若要不使用lambda

>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> filter(partial(is_not, None), L)
[0, 23, 234, 89, 0, 35, 9]
楼主石乐志崇文尚武推理极工回答于

FWIW、Python 3:

>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> list(filter(None.__ne__, L))
[0, 23, 234, 89, 0, 35, 9]

在Python 2中,可以使用列表来代替:

>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]

扫码关注云+社区