oli*_*rsm 1 python for-loop nested ternary-operator
使用Python(2.7)三元表达式x if cond else y时,按顺序嵌套这些表达式的多个表达式时,逻辑顺序是什么?
1 if A else 2 if B else 3
Run Code Online (Sandbox Code Playgroud)
为此绘制出真值表似乎被评估为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
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么要按此顺序执行,并可能建议一些可以直观地理解为什么使用/首选的材料吗?
考虑使用内联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)]
Run Code Online (Sandbox Code Playgroud)
小智 7
有人可以解释一下为什么按这个顺序执行,并可能建议一些材料来直观地了解为什么使用/首选这个?
我试图通过解决一个简单但更普遍的问题来回答你问题的“直觉”部分。
'''
+-----------------------------------------------------------------------------------+
| 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) )
#-----------------------------------------------------------------------------------+
#-----------------------------------------------------------------------------------|
#-----------------------------------------------------------------------------------|
#-----------------------------------------------------------------------------------|
#-----------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
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) )
------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
希望这个可视化能让您直观地了解如何评估嵌套三元表达式:P
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
Run Code Online (Sandbox Code Playgroud)
您的三元表达式可以用括号解释如下:
(
(1 if A) else (
(2 if B) else 3
)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1904 次 |
| 最近记录: |