[英]Combine multiple Booleans and Check if any are true in python
Title is pretty self explanatory.标题非常不言自明。 I want a condition to be satisfied if any of 16 different variables are true, but I feel like there has to be a better/cleaner way to do it than typing out if A = True or B = True .... etc etc. Is there?
如果 16 个不同变量中的任何一个为真,我想要一个条件得到满足,但我觉得必须有一种更好/更清洁的方法来做到这一点,而不是输入 if A = True 或 B = True .... 等等。在那儿?
您可以使用any()
:
any([A, B, ...])
If you want to check to see if ALL of the boolean variables are true, do the following:如果要检查所有布尔变量是否为真,请执行以下操作:
if all([length, zero_to_255, all_nums]) == True:
print("Valid IP Address")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.