【问题标题】:Python multiple nested ternary expressionPython 多重嵌套三元表达式
【发布时间】:2017-11-22 00:30:29
【问题描述】:

使用 Python (2.7) 三元表达式 x if cond else y 在计算这些按顺序嵌套的多个表达式时的逻辑顺序是什么:例如

1 if A else 2 if B else 3

为此绘制真值表似乎被评估为1 if A else (2 if B else 3)而不是(1 if A else 2) if B else 3

A      True  False
B                 
True      1      2
False     1      3

有人能解释一下为什么按这个顺序执行吗,并可能提出一些材料来直观地说明为什么使用/首选它?

在考虑使用内联 for 语句进行排序时,这似乎并不明显:

>>>[(i, j, k) for i in range(1) for j in range(2) for k in range(3)]
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2)]

【问题讨论】:

  • for 循环与三元条件不同
  • 机器人循环和三元条件从左到右求值,完全一样...
  • 这不是“inline for 语句”。
  • 哇,没有一个受访者将您的问题理解为左结合性问题 — 对于一门语法吸引无法解析的人的语言来说,这不足为奇,但仍然......

标签: python for-loop nested ternary-operator


【解决方案1】:

1 if A else 2 if B else 3 翻译成这样:

def myexpr(A, B):
    if A:
        return 1
    else:
        if B:
            return 2
        else:
            return 3

您的三元表达式可以用括号解释如下:

(
 (1 if A) else (
                (2 if B) else 3
               )
)

【讨论】:

    【解决方案2】:

    有人能解释一下为什么按这个顺序执行吗,并可能提出一些材料来直观地说明为什么使用/首选它?

    我试图通过解决一个简单但更普遍的问题来回答您问题的“直觉”部分。

    '''
    +-----------------------------------------------------------------------------------+
    |                                    Problem:                                       |
    +-----------------------------------------------------------------------------------+
    | Convert a                                                                         |
    | nested if-else block into                                                         |
    | a single line of code by using Pythons ternary expression.                        |
    | In simple terms convert:                                                          |
    |                                                                                   |
    |      1.f_nested_if_else(*args) (  which uses                                      |
    |      ````````````````````        nested if-else's)                                |
    |            |                                                                      |
    |            +--->to its equivalent---+                                             |
    |                                     |                                             |
    |                                     V                                             |
    |                              2.f_nested_ternary(*args) (     which uses           |
    |                              ```````````````````       nested ternary expression) |
    +-----------------------------------------------------------------------------------+
    '''
    '''
    Note:
    C:Conditions  (C, C1, C2)
    E:Expressions (E11, E12, E21, E22)
    Let all Conditions, Expressions be some mathematical function of args passed to the function
    '''    
    
    #-----------------------------------------------------------------------------------+
    #| 1. |      Using nested if-else                                                   |
    #-----------------------------------------------------------------------------------+
    def f_nested_if_else(*args):
        if(C):
            if(C1):
                return E11
            else:
                return E12
        else:
            if(C2):
                return E21
            else:
                return E22
    
    #-----------------------------------------------------------------------------------+
    #| 2. |      Using nested ternary expression                                        |
    #-----------------------------------------------------------------------------------+
    def f_nested_ternary(*args):
        return ( (E11) if(C1)else (E12) )   if(C)else   ( (E21) if(CF)else (E22) )
    
    
    #-----------------------------------------------------------------------------------+
    #-----------------------------------------------------------------------------------|
    #-----------------------------------------------------------------------------------|
    #-----------------------------------------------------------------------------------|
    #-----------------------------------------------------------------------------------+
    

    这是f_nested_if_else()f_nested_ternary() 等价的原因的可视化。

    #     +-----------------------------------------------------------------------------+
    #     |                               Visualization:                                |
    #     +-----------------------------------------------------------------------------+
    #     |         Visualize the ternary expression like a binary tree :               |
    #     |           -Starting from the root and  moving down to the leaves.           |
    #     |           -All the internal nodes being conditions.                         |
    #     |           -All the leaves being expressions.                                |
    #     +-----------------------------------------------------------------------------+
                                         _________________
                                         |f_nested_ternary|                                 
                                         ``````````````````
                ( (E11) if(C1)else (E12) )   if(C)else   ( (E21) if(C2)else (E22) )
                    |       |        |          |            |       |        |
                    |       |        |          |            |       |        |
                    V       V        V          V            V       V        V                                                                             
    Level-1|                  +----------------(C)-----------------+         
    --------             True/          __________________           \False         
                            V           |f_nested_if_else|            V              
    Level-2|          +----(C1)----+    ``````````````````     +----(C2)----+     
    --------     True/              \False                True/              \False
                    V                V                       V                V     
    Level-3|    ( (E11)            (E12) )               ( (E21)            (E22) ) 
    ------------------------------------------------------------------------------------+
    

    希望这个可视化让您直观地了解嵌套三元表达式的计算方式:P

    【讨论】:

      【解决方案3】:

      这两种用法都是它们相似的结构的单行同源词。 inspectorG4dget 已经为您布置了 if 版本。 for 子句按给定顺序嵌套:

      for i in range(1):
           for j in range(2):
               for k in range(3):
                   result.append( (i, j, k) )
      

      从解析器的角度来看,if 部分的工作方式相同:当它命中 1 if A 时,解析器将 if A 作为顶级决策。正如语法中定义的那样,这种嵌套是从右到左绑定的:if B 是最内层(inner-more?)。

      【讨论】:

        【解决方案4】:

        布尔谓词在许多语言中被定义为尽快终止,只要知道最终结果,特别是如果左侧是真的。它实际上与列表理解中发生的解构赋值无关。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-24
        • 2015-06-02
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        • 2018-02-26
        • 2020-06-17
        • 1970-01-01
        相关资源
        最近更新 更多