gpt4 book ai didi

python - 立即检查一组变量的 boolean 值

转载 作者:太空狗 更新时间:2023-10-30 01:55:19 25 4
gpt4 key购买 nike

我有大约 10 个 boolean 变量,如果所有这十个变量值都是 True,我需要设置一个新的 boolean 变量 x=True。如果其中一个为 False,则设置 x = False 我可以用某种方式做到这一点

if (a and b and c and d and e and f...):
x = True
else:
x=False

这显然看起来很丑。请提出更多 pythonic 解决方案。

丑陋的部分是a and b and c and d and e and f...

最佳答案

假设您在列表/元组中有 boolean 值:

x = all(list_of_bools)

或者正如@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

关于python - 立即检查一组变量的 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11964979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com