在检查了一次布尔值从一组变量 [英] Check at once the boolean values from a set of variables
本文介绍了在检查了一次布尔值从一组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有大约10布尔变量,我需要设置一个新的布尔变量 X =真
如果所有这十个变量的值是True.If其中一个是假然后设置 X =假
我的方式做到这一点。
I am having around 10 boolean variables, I need to set a new boolean variable x=True
if all those ten variable values are True.If one of them is False then set x= False
I can do this in a manner
if (a and b and c and d and e and f...):
x = True
else:
x=False
这显然看起来很ugly.Please提出了一些更Python的解决方案。
which obviously looks very ugly.Please suggest some more pythonic solutions.
丑陋的部分是 a和b和c和d和e和f ...
推荐答案
假设你有一个列表/元组的bool:
Assuming you have the bools in a list/tuple:
x = all(list_of_bools)
或只是由美浓@ preT
or just as suggested by @minopret
x= all((a, b, c, d, e, f))
例如:
>>> list_of_bools = [True, True, True, False]
>>> all(list_of_bools)
False
>>> list_of_bools = [True, True, True, True]
>>> all(list_of_bools)
True
这篇关于在检查了一次布尔值从一组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文