Python 多个嵌套三元表达式



使用 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)]

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

有人可以解释为什么按此顺序执行,并可能建议一些材料来直观地说明为什么使用/首选?

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

'''
+-----------------------------------------------------------------------------------+
|                                    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(C2)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

这两种用法都是它们所相似结构的单行同源词。 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是最内层的(更内在的?(。

布尔

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

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

最新更新