bai*_*ibo 1 python logic equivalence control-flow
我严重睡眠不足,我需要帮助重写这个小的Python逻辑
for _ in range(100):
if a:
continue
elif b:
continue
elif c and d:
continue
else:
e()
Run Code Online (Sandbox Code Playgroud)
我希望有类似的东西
if (some_exprt of a,b,c,d):
e()
Run Code Online (Sandbox Code Playgroud)
我得到的是:
if not a and not b and (not c or not d):
e()
Run Code Online (Sandbox Code Playgroud)
但是我真的不知道这是否正确,我是对的吗?
从else分支不匹配的条件开始.它是a或者b,或者,或者,或者c and d,所以你需要使用or并not在这里表达else原始代码的分支何时被选中:
if not (a or b or (c and d)):
e()
Run Code Online (Sandbox Code Playgroud)
然后,您可以not通过应用De Morgan的一个定律将其引入括号,将前面的测试更详尽地表达为:
if not a and not b and not (c and d):
e()
Run Code Online (Sandbox Code Playgroud)
然后可以进一步扩展到:
if not a and not b and (not c or not d):
e()
Run Code Online (Sandbox Code Playgroud)
这是你自己已经扩展到的.但我发现第一个版本更具可读性.