296

这是我开始的来源。

我的列表

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 有很大的不同。

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

4

11 回答 11

439
>>> 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]
于 2013-04-19T03:33:06.840 回答
152

列表理解可能是最干净的方式:

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

还有一种函数式编程方法,但涉及更多:

>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> list(filter(partial(is_not, None), L))
[0, 23, 234, 89, 0, 35, 9]
于 2013-04-19T04:16:12.357 回答
22

使用列表推导可以如下完成:

l = [i for i in my_list if i is not None]

l 的值为:

[0, 23, 234, 89, 0, 35, 9]
于 2013-04-19T03:34:38.307 回答
17

@jamylak 的回答非常好,但是如果您不想导入几个模块只是为了完成这个简单的任务,请lambda就地编写自己的:

>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> filter(lambda v: v is not None, L)
[0, 23, 234, 89, 0, 35, 9]
于 2014-10-25T07:52:33.830 回答
16

对于 Python 2.7(请参阅 Raymond 的回答,对于 Python 3 等效):

想知道“不是无”在python(和其他OO语言)中是否如此普遍,以至于在我的Common.py(我用“from Common import *”导入每个模块)中,我包括了以下几行:

def exists(it):
    return (it is not None)

然后要从列表中删除 None 元素,只需执行以下操作:

filter(exists, L)

我发现这比相应的列表理解(Raymond 显示为他的 Python 2 版本)更容易阅读。

于 2013-12-13T03:22:11.373 回答
5

Iteration vs Space,使用可能是一个问题。在不同的情况下,分析可能显示“更快”和/或“内存更少”密集。

# first
>>> 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, ...]

# second
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> for i in range(L.count(None)): L.remove(None)
[0, 23, 234, 89, 0, 35, 9, ...]

一种方法(@jamylak@Raymond Hettinger@Dipto也建议)在内存中创建一个重复列表,这对于包含少量None条目的大型列表可能会占用大量内存。

第二种方法遍历列表一次,然后每次都遍历一次,直到None到达 a。这可能会占用更少的内存,并且列表会随着时间的推移而变得更小。列表大小的减少可能会加快None前面的大量条目,但最坏的情况是如果None后面有很多条目。

第二种方法可能总是比第一种方法慢。这并不意味着它是一个无效的考虑。

并行化和就地技术是其他方法,但每种方法在 Python 中都有其自身的复杂性。了解数据和运行时用例以及分析程序是密集操作或大数据的起点。

在常见情况下,选择任何一种方法都可能无关紧要。它变得更像是一种符号偏好。事实上,在那些不常见的情况下,numpy(例如,如果 L 是 numpy.array: L = L[L != numpy.array(None)( from here ))或者cython可能是值得的替代方案,而不是尝试对 Python 优化进行微观管理。

于 2015-06-08T20:35:30.007 回答
4

说列表如下

iterator = [None, 1, 2, 0, '', None, False, {}, (), []]

这将只返回那些bool(item) is True

print filter(lambda item: item, iterator)
# [1, 2]

这相当于

print [item for item in iterator if item]

只过滤无:

print filter(lambda item: item is not None, iterator)
# [1, 2, 0, '', False, {}, (), []]

相当于:

print [item for item in iterator if item is not None]

获取所有评估为 False 的项目

print filter(lambda item: not item, iterator)
# Will print [None, '', 0, None, False, {}, (), []]
于 2017-05-22T07:47:19.613 回答
3
from operator import is_not
from functools import partial   

filter_null = partial(filter, partial(is_not, None))

# A test case
L = [1, None, 2, None, 3]
L = list(filter_null(L))
于 2016-11-01T23:43:41.623 回答
3
L = [0, 23, 234, 89, None, 0, 35, 9] 
result = list(filter(lambda x: x is not None, L))
于 2020-07-09T06:07:27.863 回答
2

如果都是列表列表,您可以修改@Raymond先生的答案

L = [ [None], [123], [None], [151] ] no_none_val = list(filter(None.__ne__, [x[0] for x in L] ) ) 但是对于 python 2

no_none_val = [x[0] for x in L if x[0] is not None] """ Both returns [123, 151]"""

<< list_indice[0] 用于 List 中的变量,如果变量不是 None >>

于 2017-09-10T13:54:50.097 回答
2

如果列表有 NoneType 和 pandas._libs.missing.NAType 对象,则使用:

[i for i in lst if pd.notnull(i)]
于 2021-10-01T15:27:59.423 回答