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值?
10 个解决方案
#1
226
>>> 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 filter
to 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]
#2
78
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]
#3
15
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:
然后,从列表中删除所有元素,只需:
filter(exists, L)
I find this easier to read, than the corresponding list comprehension (which Raymond shows, as his Python 2 version).
我发现这比相应的列表理解更容易阅读(雷蒙德在他的Python 2版本中显示了这一点)。
#4
10
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]
#5
8
@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 lambda
in-place:
@jamylak的回答很不错,但是如果你不想仅仅为了完成这个简单的任务而导入几个模块,那么写你自己的lambda in-place:
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> filter(lambda v: v is not None, L)
[0, 23, 234, 89, 0, 35, 9]
#6
4
Iteration vs Space, usage could be an issue. In different situations profiling may show either to be "faster" and/or "less memory" intensive.
迭代vs空间,使用可能是一个问题。在不同的情况下,性能分析可能显示“更快”或“内存更少”。
# 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 first approach (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 None
entries.
第一种方法(正如@jamylak、@Raymond Hettinger和@Dipto所建议的那样)在内存中创建一个重复的列表,对于一个几乎没有条目的大列表来说,这可能是昂贵的。
The second approach goes through the list once, and then again each time until a None
is 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 None
entries in the front, but the worst case would be if lots of None
entries were in the back.
第二种方法遍历列表一次,然后再遍历一次,直到达到一个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, numpy
or cython
may be worthwhile alternatives instead of attempting to micromanage Python optimizations.
在一般情况下,选择任何一种方法都可能无关紧要。它更倾向于符号。事实上,在那些不常见的情况下,numpy或cython可能是值得的替代方案,而不是试图对Python优化进行微观管理。
#7
2
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))
#8
1
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(项目)为真的项目
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, {}, (), []]
#9
1
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)。然而,对于python 2, [x[0]在L中表示x])
no_none_val = [x[0] for x in L if x[0] is not None] """ Both returns [123, 151]"""
no_none_val = [x[0]在L中的x如果x[0]不是None] """都返回[123,151]""" "
<< list_indice[0] for variable in List if variable is not None >>
<列表中的变量,如果变量不是没有,则< list_indice[0]< p>
#10
-3
This solution uses while rather than for:
这个解决方案使用while而不是for:
value = None
while value in yourlist:
yourlist.remove(value)
#1
226
>>> 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 filter
to 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]
#2
78
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]
#3
15
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:
然后,从列表中删除所有元素,只需:
filter(exists, L)
I find this easier to read, than the corresponding list comprehension (which Raymond shows, as his Python 2 version).
我发现这比相应的列表理解更容易阅读(雷蒙德在他的Python 2版本中显示了这一点)。
#4
10
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]
#5
8
@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 lambda
in-place:
@jamylak的回答很不错,但是如果你不想仅仅为了完成这个简单的任务而导入几个模块,那么写你自己的lambda in-place:
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> filter(lambda v: v is not None, L)
[0, 23, 234, 89, 0, 35, 9]
#6
4
Iteration vs Space, usage could be an issue. In different situations profiling may show either to be "faster" and/or "less memory" intensive.
迭代vs空间,使用可能是一个问题。在不同的情况下,性能分析可能显示“更快”或“内存更少”。
# 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 first approach (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 None
entries.
第一种方法(正如@jamylak、@Raymond Hettinger和@Dipto所建议的那样)在内存中创建一个重复的列表,对于一个几乎没有条目的大列表来说,这可能是昂贵的。
The second approach goes through the list once, and then again each time until a None
is 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 None
entries in the front, but the worst case would be if lots of None
entries were in the back.
第二种方法遍历列表一次,然后再遍历一次,直到达到一个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, numpy
or cython
may be worthwhile alternatives instead of attempting to micromanage Python optimizations.
在一般情况下,选择任何一种方法都可能无关紧要。它更倾向于符号。事实上,在那些不常见的情况下,numpy或cython可能是值得的替代方案,而不是试图对Python优化进行微观管理。
#7
2
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))
#8
1
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(项目)为真的项目
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, {}, (), []]
#9
1
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)。然而,对于python 2, [x[0]在L中表示x])
no_none_val = [x[0] for x in L if x[0] is not None] """ Both returns [123, 151]"""
no_none_val = [x[0]在L中的x如果x[0]不是None] """都返回[123,151]""" "
<< list_indice[0] for variable in List if variable is not None >>
<列表中的变量,如果变量不是没有,则< list_indice[0]< p>
#10
-3
This solution uses while rather than for:
这个解决方案使用while而不是for:
value = None
while value in yourlist:
yourlist.remove(value)