[英]Establish empty tuple in tuple of tuples
How can I do a test that returns true if no tuple within a tuple of tuples is empty? 如果在一个元组的元组中没有空的元组,我该如何做一个返回true的测试?
For example, return True
in this case: 例如,在这种情况下,返回True
:
(('t2',), ('t3',), ('t4',), ('t5','t6'))
return False
in this case: 在这种情况下,返回False
:
(('t2',), (), ('t3',), ('t4',))
Please give your answer so that it is valid for Python3. 请给出答案,以使其对Python3有效。
You can use the built-in all
function as empty tuple is falsey in Python: 您可以使用内置的all
函数,因为Python中的空元组是false:
Help on built-in function all in module builtins:
all(...)
all(iterable) -> bool
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
>>> all((('t2',), ('t3',), ('t4',), ('t5', 't6')))
True
>>> all((('t2',), (), ('t3',), ('t4',)))
False
The opposite of "no tuple within a tuple of tuples is empty" is "some tuple... is empty"; “在元组的元组中没有元组为空”的反义是“某些元组...为空”; equivalently, "an empty tuple can be found within the tuple of tuples". 等效地,“可以在元组的元组中找到一个空元组”。
This naturally leads to an equally simple (and, I think, marginally more readable) but quite different solution: 这自然导致一个同样简单的解决方案(并且,我认为,它的可读性稍高),但解决方案却大不相同:
>>> () not in (('t2',), ('t3',), ('t4',), ('t5', 't6'))
True
>>> () not in (('t2',), (), ('t3',), ('t4',))
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.