【问题标题】:Logical equivalence of a sequence of if else if else clausesif else if else 子句序列的逻辑等价
【发布时间】:2013-12-10 10:15:18
【问题描述】:

我严重睡眠不足,我需要帮助来重写这个小的 Python 逻辑

for _ in range(100):
    if a:
        continue
    elif b:
        continue
    elif c and d:
        continue
    else:
        e()

我想要类似的东西

if (some_exprt of a,b,c,d):
    e()

我得到的是:

if not a and  not b and (not c or not d):
   e()

但我真的不知道这是否正确,对吗?

【问题讨论】:

  • 你到底想做什么?
  • 试着从“自下而上”的角度思考它。 "如果 a 或 b 或 c 和 d 都不为真,则执行 e()"
  • 您可以使用De_Morgan's_laws 将您的答案转换为@Martijn 给出的答案
  • @InbarRose 循环和跳过不可行的迭代
  • 简单的条件比复杂的条件好,continue 也很好(参见例如llvm.org/docs/…)。只需将elifs 替换为ifs 即可。

标签: python logic control-flow equivalence


【解决方案1】:

else 分支匹配的条件开始。它将是abc and d 之一,因此您需要在此处使用ornot 来表示何时选择原始代码的else 分支:

if not (a or b or (c and d)):
    e()

然后,您可以通过应用one of De Morgan's lawsnot 带入括号,将前面的测试更详细地表示为:

if not a and not b and not (c and d):
    e()

然后可以进一步扩展为:

if not a and not b and (not c or not d):
    e()

这是您自己已经扩展的内容。但我会发现第一个版本更具可读性。

【讨论】:

    【解决方案2】:
    if not any((a, b, (c and d))):
        e()
    

    【讨论】:

    • 这将在调用any() 之前评估abc(可能还有d)。当a 评估为假值时,这将不会短路。这可能很重要。
    【解决方案3】:

    continue 不能在 if 语句中工作。所以我假设你在循环中运行它(while 或 for)。试试这个:

    #whatever loop
    if not(a or b or (c and d)):
        e()
    

    不带not的第二种方法是:

    if a or b or (c and d):
        continue
    else:
        e()
    

    正如 M. Martjin Peters 在 cmets 中所解释的,第二种方法中的 else 块是不必要的。您可以删除 else 并将 e() 移到 if 块之外。但是,在我看来,if 之后的 else 将使代码更具可读性。

    第二种方法也可以写成:

    if a or b or (c and d):
        continue
    e()
    

    【讨论】:

    • 我是,我来展开上下文
    • 我想我们可以在这里假设if 语句是循环中唯一的代码。
    • 这是安全的假设 :-) 我只是想向 OP 澄清,如果他正在尝试,继续不能在 if 块中使用 :-)
    • 如果您继续无论如何,您可以放弃else 并取消e() 呼叫。 if a or b or (c and d): continue 换行 e().
    • @MartijnPieters 没错。谢谢。我会更新代码。谢谢。
    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 2018-03-12
    • 2016-03-24
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多