[英]How to check if all items in the list are None?
In [27]: map( lambda f,p: f.match(p), list(patterns.itervalues()), vatids )
Out[27]: [None, <_sre.SRE_Match object at 0xb73bfdb0>, None]
The list can be all None
or one of it is an re.Match instance.该列表可以全部为
None
或其中之一是 re.Match 实例。 What one liner check can I do on the returned list to tell me that the contents are all None
?我可以在退回的清单上做哪一项检查来告诉我内容都是
None
?
all(v is None for v in l)
will return True
if all of the elements of l
are None
如果
l
的所有元素都是None
将返回True
Note that l.count(None) == len(l)
is a lot faster but requires that l
be an actual list
and not just an iterable.请注意,
l.count(None) == len(l)
要快得多,但要求l
是一个实际的list
,而不仅仅是一个可迭代的。
not any(my_list)
returns True
if all items of my_list
are falsy.如果
my_list
的所有项目都是假的,则返回True
。
Edit : Since match objects are always trucy and None
is falsy, this will give the same result as all(x is None for x in my_list)
for the case at hand.编辑:由于匹配对象总是 trucy 并且
None
是虚假的,因此对于手头的情况,这将给出与all(x is None for x in my_list)
相同的结果。 As demonstrated in gnibbler's answer , using any()
is by far the faster alternative.正如gnibbler 的回答所示,使用
any()
是迄今为止更快的选择。
Since Match objects are never going to evaluate to false, it's ok and much faster to just use not any(L)
由于 Match 对象永远不会评估为 false,因此使用
not any(L)
可以而且更快
$ python -m timeit -s"L=[None,None,None]" "all( v is None for v in L )"
100000 loops, best of 3: 1.52 usec per loop
$ python -m timeit -s"L=[None,None,None]" "not any(L)"
1000000 loops, best of 3: 0.281 usec per loop
$ python -m timeit -s"L=[None,1,None]" "all( v is None for v in L )"
100000 loops, best of 3: 1.81 usec per loop
$ python -m timeit -s"L=[None,1,None]" "not any(L)"
1000000 loops, best of 3: 0.272 usec per loop
Or a bit weird but:或者有点奇怪,但是:
a = [None, None, None]
set(a) == set([None])
OR:或者:
if [x for x in a if x]: # non empty list
#do something
EDITED:编辑:
def is_empty(lVals):
if not lVals:
return True
for x in lVals:
if x:
return False
return True
I managed to come up with an approach using map
that hasn't been given yet我设法想出一种使用尚未给出的
map
的方法
def all_none(l):
return not any(map(None.__ne__, l))
all_none([None, None, None]) # -> True
all_none([None, None, 8]) # -> False
The use of None.__ne__
is bit weirder than you'd expect at first glance.乍一看,
None.__ne__
的使用比你想象的要奇怪。 This method returns a NotImplementedType
object when given something that isn't None.当给定的东西不是 None 时,此方法返回
NotImplementedType
object。
You'd be hoping that NotImplemented
would be a stand-in for False
, however it's truthy too!您可能希望
NotImplemented
成为False
的替代品,但它也是真实的! This means that using None.__eq__
across a collection will produce thruthy values for everything.这意味着在集合中使用
None.__eq__
将为所有内容产生真实的值。
list(map(None.__eq__, [None, None, 8]))
# -> [True, True, NotImplemented]
all(list(map(None.__eq__, [None, None, 8])))
# -> True
From the Python Docs :来自Python 文档:
By default, an object is considered true unless its class defines either a bool () method that returns False or a len () method that returns zero
默认情况下,object 被认为是 true,除非它的 class 定义了返回 False 的bool () 方法或返回零的len () 方法
Instead, None.__ne__
returns False
for any None
elements, and a NotImplementedType
object for anything else:相反,
None.__ne__
为任何None
元素返回False
,为任何其他元素返回NotImplementedType
object :
list(map(None.__ne__, [None, None, 8]))
# -> [False, False, NotImplemented]
Using this, you can check if any
elements are not None
which will return True
.使用它,您可以检查是否
any
元素不是返回True
的None
。 I like to think of this as 2 separate methods if that helps with the mental negation gymnastics.如果这有助于心理否定体操,我喜欢将其视为两种不同的方法。
def contains_truthy(l):
return any(map(None.__ne__, l))
def all_none(l):
return not contains_truthy(l)
I haven't done any benchmarking with this, but as mentioned by others in this thread, not any
will produce fast results.我没有对此进行任何基准测试,但正如该线程中的其他人所提到的,
not any
会产生快速的结果。
is_all_none = lambda L: not len(filter(lambda e: not e is None, L))
is_all_none([None,None,'x'])
False
is_all_none([None,None,None])
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.