我如何测试 python 列表中的空列表条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19252588/
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
How do I test for null list entry in python list
提问by scopedial
I have a python list [1,2,,4,5,6]
. What is the best method for testing for the missing, or null
, list element? I am currently performing a if element != ''
but I think there is a built-in test to perform such a thing, or am I wrong?
我有一个 python 列表[1,2,,4,5,6]
。测试缺失或null
列表元素的最佳方法是什么?我目前正在执行 aif element != ''
但我认为有一个内置的测试来执行这样的事情,还是我错了?
采纳答案by Ewan
Pythonic ways for checking for None
or null
are:
检查None
或 的Pythonic 方法null
是:
if element:
# This is not null
if not element:
# This is null
There is a very detailed answeron the difference between if not x
and if x == None
.
有一个非常详细的回答上的区别if not x
和if x == None
。
Edit 1:
编辑1:
Combining the comment and the other answers:
结合评论和其他答案:
False Values
错误值
Python treats the following as False
source:
Python 将以下内容视为False
源代码:
None
False
- zero of any numeric type, eg:
0
,0L
,0.0
,0j
- any empty sequence, eg:
''
,()
,[]
- any empty mapping, eg:
{}
- instances of user-defined classes, if the class defines a
__nonzero__()
or__len__()
method, when that method returns the integerzero
or bool valueFalse
None
False
- 任何数字类型的零,例如:
0
,0L
,0.0
,0j
- 任何空序列,例如:
''
,()
,[]
- 任何空映射,例如:
{}
- 用户定义类的实例,如果类定义了一个
__nonzero__()
或__len__()
方法,当该方法返回整数zero
或布尔值时False
True Values
真实值
All other values are considered to be True
.
所有其他值都被视为True
。
Your question:
你的问题:
If you are indeed checking if None
is present in your list ([1,2,None,4,5,6]
) then @Poke 's answer is right:
如果您确实要检查None
列表中是否存在 ( [1,2,None,4,5,6]
),那么@Poke 的答案是正确的:
>>> lst = [1, 2, None, 4, 5, 6]
>>> None in lst
True
If you are wanting to check that the element is onlyNone
then @esauro is correct in the comments:
如果您想检查该元素是否仅None
在评论中@esauro 是正确的:
>>> lst = [1, 2, None, 4, 5, 6]
>>> for x in lst:
... if not x:
... print(x)
None
But if your lst
contains 0
(lst = [0, 1, 2, 4, 5, 6]) then your output will be 0
.
但是如果你lst
包含0
(lst = [0, 1, 2, 4, 5, 6]) 那么你的输出将是0
.
The only way you could get round this would be to explicitly check if element is None
or if element is not None
.
您可以绕过此问题的唯一方法是明确检查if element is None
或if element is not None
。
回答by poke
If your list is [1, 2, None, 4, 5, 6]
then you can just check None in lst
, e.g.:
如果您的列表是,[1, 2, None, 4, 5, 6]
那么您只需检查None in lst
,例如:
>>> lst = [1, 2, None, 4, 5, 6]
>>> None in lst
True
Similarly, if you have some other value that represents a “missing” entry, you can just check whatever in lst
.
同样,如果您有其他一些代表“缺失”条目的值,您只需检查whatever in lst
.
回答by Emil Davtyan
Find all the indexes where there is a None
:
找到所有有 的索引None
:
[i for i, item in enumerate(mylist) if item is None]
Example output :
示例输出:
>>> mylist = [1, 2, None, 4, 5, 6, None, None]
>>> [i for i, item in enumerate(mylist) if item is None]
[2, 6, 7]
回答by Burhan Khalid
I have a python list [1,2,,4,5,6]. What is the best method for testing for the missing, or null, list element?
我有一个 python 列表 [1,2,,4,5,6]。测试缺失或空列表元素的最佳方法是什么?
If you just want to know if the list contains a falsey value(that's the best equivalent for missing or nullin Python), you can do this:
如果您只想知道列表是否包含falsey 值(这是Python 中缺失或 null的最佳等价物),您可以这样做:
>>> all(x for x in [1,2,3,4,5])
True
>>> all(x for x in [1,2,'',4,5])
False
>>> all(x for x in [1,2,None,4,5])
False
If you want to know if a specific value exists that matches some condition:
如果您想知道是否存在与某些条件匹配的特定值:
all(x%2 for x in [1,2,3,4,5,7])
This will be true if the list contains all even numbers.
如果列表包含所有偶数,则为真。