Python 多个嵌套的三元表达式

Python multiple nested ternary expression

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

1 if A else 2 if B else 3

得出真相 table 因为这似乎被评估为 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

谁能解释一下为什么按这个顺序执行,并可能提出一些 material 的直觉来解释为什么这是 used/preferred?

考虑使用内联 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)]

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
               )
)

布尔谓词在许多语言中被定义为尽快终止,只要知道最终结果,尤其是如果 or 表达式的左侧根本不会计算其右侧侧面是真的。它确实与列表推导中发生的解构赋值无关。

这两种用法都是它们相似结构的同源词。 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?)。

Could someone please explain why this is executed in this order, and possibly suggest some material that gives an intuition about why this is used/preferred?

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

'''
+-----------------------------------------------------------------------------------+
|                                    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