检查元组在 Python 中是否有任何空/无值的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31154372/
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-19 09:33:56  来源:igfitidea点击:

What is the best way to check if a tuple has any empty/None values in Python?

pythontuplesis-empty

提问by Andrius

What is the best/most efficient way to check if all tuple values? Do I need to iterate over all tuple items and check or is there some even better way?

检查所有元组值的最佳/最有效方法是什么?我是否需要遍历所有元组项并进行检查,还是有更好的方法?

For example:

例如:

t1 = (1, 2, 'abc')
t2 = ('', 2, 3)
t3 = (0.0, 3, 5)
t4 = (4, 3, None)

Checking these tuples, every tuple except t1, should return True, meaning there is so called empty value.

检查这些元组,除了 ,每个元组都t1应该返回 True,这意味着存在所谓的空值。

P.S. there is this question: Test if tuple contains only None values with Python, but is it only about None values

PS 有这个问题:测试 tuple 是否只包含使用 Python 的 None 值,但是否仅包含None 值

采纳答案by Tim Pietzcker

It's very easy:

这很容易:

not all(t1)

returns Falseonly if all values in t1are non-empty/nonzero and not None. allshort-circuits, so it only has to check the elements up to the first empty one, which makes it very fast.

False仅当 中的所有值t1都为非空/非零且 not时才返回Noneall短路,所以它只需要检查第一个空的元素,这使得它非常快。

回答by Anand S Kumar

For your specific case, you can use all()function , it checks all the values of a list are true or false, please note in python None, empty string and 0are considered false.

对于您的特定情况,您可以使用all()function ,它检查列表的所有值是真还是假,请注意在 python 中None,空字符串0被视为假。

So -

所以 -

>>> t1 = (1, 2, 'abc')
>>> t2 = ('', 2, 3)
>>> t3 = (0.0, 3, 5)
>>> t4 = (4, 3, None)
>>> all(t1)
True
>>> all(t2)
False
>>> all(t3)
False
>>> all(t4)
False
>>> if '':
...     print("Hello")
...
>>> if 0:
...     print("Hello")

回答by Grismar

An answer using all has been provided:

已经提供了使用 all 的答案:

not all(t1)

However in a case like t3, this will return True, because one of the values is 0:

但是在像 t3 这样的情况下,这将返回 True,因为其中一个值为 0:

t3 = (0.0, 3, 5)

The 'all' built-in keyword checks if all values of a given iterable are values that evaluate to a negative boolean (False). 0, 0.0, '' and None all evaluate to False.

'all' 内置关键字检查给定可迭代对象的所有值是否都是评估为负布尔值 (False) 的值。0、0.0、'' 和 None 都评估为 False。

If you only want to test for None (as the title of the question suggests), this works:

如果您只想测试 None (如问题的标题所示),这有效:

any(map(lambda x: x is None, t3))

This returns True if any of the elements of t3 is None, or False if none of them are.

如果 t3 的任何元素为 None,则返回 True,否则返回 False。

回答by tsveti_iko

If by any chance want to check if there is an empty value in a tuple containing tuples like these:

如果有任何机会想要检查包含如下元组的元组中是否有空值:

t1 = (('', ''), ('', ''))
t2 = ((0.0, 0.0), (0.0, 0.0))
t3 = ((None, None), (None, None))

you can use this:

你可以使用这个:

not all(map(lambda x: all(x), t1))

Otherwise if you want to know if there is at least one positive value, then use this:

否则,如果您想知道是否至少有一个正值,请使用以下命令:

any(map(lambda x: any(x), t1))