Python 从列表中删除 None 值而不删除 0 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16096754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:45:34  来源:igfitidea点击:

remove None value from a list without removing the 0 value

pythonlistfilternonetype

提问by mongotop

This was my source I started with.

这是我开始的来源。

My List

我的列表

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

When I run this :

当我运行这个:

L = filter(None, L)

I get this results

我得到这个结果

[23, 234, 89, 35, 9]

But this is not what I need, what I really need is :

但这不是我需要的,我真正需要的是:

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

Because I'm calculating percentile of the data and the 0 make a lot of difference.

因为我正在计算数据的百分位数,而 0 有很大的不同。

How to remove the None value from a list without removing 0 value?

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

采纳答案by jamylak

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

Just for fun, here's how you can adapt filterto do this without using a lambda, (I wouldn't recommend this code - it's just for scientific purposes)

只是为了好玩,这里是您如何在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]

回答by DotPi

Using list comprehension this can be done as follows:

使用列表理解可以按如下方式完成:

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

The value of l is:

l 的值为:

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

回答by Raymond Hettinger

FWIW, Python 3 makes this problem easy:

FWIW,Python 3 使这个问题变得简单:

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

In Python 2, you would use a list comprehension instead:

在 Python 2 中,您将改用列表推导式:

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

回答by ToolmakerSteve

For Python 2.7 (See Raymond's answer, for Python 3 equivalent):

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

Wanting to know whether something "is not None" is so common in python (and other OO languages), that in my Common.py (which I import to each module with "from Common import *"), I include these lines:

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

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

Then to remove None elements from a list, simply do:

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

filter(exists, L)

I find this easier to read, than the corresponding list comprehension (which Raymond shows, as his Python 2 version).

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

回答by A T

@jamylak answer is quite nice, however if you don't want to import a couple of modules just to do this simple task, write your own lambdain-place:

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

回答by Kevin

Iterationvs Space, usage could be an issue. In different situations profiling may show either to be "faster" and/or "less memory" intensive.

Iterationvs 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, ...]

The firstapproach (as also suggested by @jamylak, @Raymond Hettinger, and @Dipto) creates a duplicate list in memory, which could be costly for a large list with few Noneentries.

一种方法(也由@jamylak@Raymond Hettinger@Dipto 建议)在内存中创建一个重复的列表,这对于None条目很少的大列表来说可能代价高昂。

The secondapproach goes through the list once, and then again each time until a Noneis reached. This could be less memory intensive, and the list will get smaller as it goes. The decrease in list size could have a speed up for lots of Noneentries in the front, but the worst case would be if lots of Noneentries were in the back.

第二个方法经过列表一次,然后再次每次直至None到达。这可能会占用较少的内存,并且列表会随着它的进行而变得更小。列表大小的减少可能会加快None前面的大量条目,但最坏的情况是如果None后面的条目很多。

Parallelization and in-place techniques are other approaches, but each have their own complications in Python. Knowing the data and the runtime use-cases, as well profiling the program are where to start for intensive operations or large data.

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

Choosing either approach will probably not matter in common situations. It becomes more of a preference of notation. In fact, in those uncommon circumstances, numpyor cythonmay be worthwhile alternatives instead of attempting to micromanage Python optimizations.

在常见情况下,选择任何一种方法都可能无关紧要。它更像是一种符号的偏好。事实上,在那些不常见的情况下,numpy或者cython可能是值得的替代方案,而不是尝试微观管理 Python 优化。

回答by med_abidi

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

回答by theBuzzyCoder

Say the list is like below

说清单如下

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

This will return only those items whose bool(item) is True

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

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

This is equivalent to

这相当于

print [item for item in iterator if item]

To just filter None:

只过滤无:

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

Equivalent to:

相当于:

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

To get all the items that evaluate to False

获取所有评估为 False 的项目

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

回答by Skrmnghrd

If it is all a list of lists, you could modify sir @Raymond's answer

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

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

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

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

<< list_indice[0] for variable in List if variable is not None >>

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