在两组布尔值和字符串表示之间切换

Toggling between two sets of boolean and string representations

我有 2 个布尔变量:

a = True
b = True

并且每个都有一些属性,这些属性体现在它们的字符串表示中:

a_str = "a is real"
b_str = "b is surreal"

结果应该是:

我已经尝试了以下并且它有效但是引出不同的 if-elifs 相当冗长,即

a = True
b = True

a_str = "a is real"
b_str = "b is surreal"

if a and b:
    print(f"{a_str} AND {b_str}")
elif a:
    print(a_str)
elif b:
    print(b_str)

尤其是。如果有一个新变量 c,例如

a = True
b = True
c = True

a_str = "a is real"
b_str = "b is surreal"
c_str = "c is cereal"

if a and b and c:
    print(f"{a_str} AND {b_str} AND {c_str")
elif a and b:
     print(f"{a_str} AND {b_str}")
elif a and c:
     print(f"{a_str} AND {b_str}")
elif b and c:
     print(f"{b_str} AND {c_str}")
elif a:
     print(a_str)
elif b:
     print(b_str)
elif c:
     print(c_str)

是否有更简洁的方法来枚举布尔检查的不同情况?

你不能做类似的事情吗:

' AND '.join(filter(None,[a_str*a,b_str*b,c_str*c]))

为什么不将这些值放在两个不同的列表中。一个带有布尔值,另一个带有要打印的字符串。

然后您可以使用与此 post 描述相同的逻辑来获得第三个列表,该列表的值的索引为 True:

现在用“and”加入第三个列表。