在Python中是否有特纳利条件操作器?
当前回答
Vinko Vrsalovic的答案是足够好的,只有一件事:
請注意,條件是表達,而不是表達,這意味著您無法在條件表達內使用任命表達或通過或其他表達。
华鲁斯操作员在Python 3.8
在Walrus操作器在Python 3.8中引入后,有些事情发生了变化。
(a := 3) if True else (b := 5)
给 a = 3 和 b 不定义,
(a := 3) if False else (b := 5)
给 a 不定义 和 b = 5 和
c = (a := 3) if False else (b := 5)
给 c = 5, a 不定义, b = 5 。
即使这可能是丑陋的,任务可以在Python 3.8之后的条件表达中进行。
其他回答
从文档中:
条件表达式(有时称为“永久运营商”)具有所有 Python 操作的最低优先事项. 表达式 x 如果 C 其他 y 首先评估条件, C (不是 x); 如果 C 是真实的, x 被评估并返回值; 否则, y 被评估并返回值。
此分類上一篇: 最新版本 2.5.
是的,它有,但它与C类似的编程语言不同(这是条件? value_if_true : value_if_false
在 Python 中,它如下: value_if_true 如果另一个条件值_if_false
例如: even_or_odd = “even” 如果 x % 2 == 0 其他 “odd”
许多从 C 产生的编程语言通常具有以下条件运营商的合成:
<condition> ? <expression1> : <expression2>
起初,Python的善良独裁者为生命(我指的是Guido van Rossum,当然)拒绝它(作为非Pythonic风格),因为它是相当难以理解的人不使用C语言。
<expression1> if <condition> else <expression2>
因此,首先,它评估了状态. 如果它返回真,表达1将被评估给结果,否则表达2将被评估。
下面是几个例子(条件将从左向右评估):
pressure = 10
print('High' if pressure < 20 else 'Critical')
# Result is 'High'
Ternary 运营商可以分为序列:
pressure = 5
print('Normal' if pressure < 10 else 'High' if pressure < 20 else 'Critical')
# Result is 'Normal'
下一个与前一个相同:
pressure = 5
if pressure < 20:
if pressure < 10:
print('Normal')
else:
print('High')
else:
print('Critical')
# Result is 'Normal'
你可能经常发现
cond and on_true or on_false
但这会导致一个问题,当 on_true == 0
>>> x = 0
>>> print x == 0 and 0 or 1
1
>>> x = 1
>>> print x == 0 and 0 or 1
1
在哪里你会期望这个结果为一个正常的特纳利运营商:
>>> x = 0
>>> print 0 if x == 0 else 1
0
>>> x = 1
>>> print 0 if x == 0 else 1
1
正如我已经回答过的那样,是的,在Python中有一个特纳里操作员:
<expression 1> if <condition> else <expression 2>
在许多情况下, < 表达 1> 也被用作 Boolean 评估的 < 条件>. 然后您可以使用短循环评估。
a = 0
b = 1
# Instead of this:
x = a if a else b
# Evaluates as 'a if bool(a) else b'
# You could use short-circuit evaluation:
x = a or b
短循环评估的一个大专业是链接超过两个表达式的可能性:
x = a or b or c or d or e
当与功能工作时,它在细节上更不同:
# Evaluating functions:
def foo(x):
print('foo executed')
return x
def bar(y):
print('bar executed')
return y
def blubb(z):
print('blubb executed')
return z
# Ternary Operator expression 1 equals to False
print(foo(0) if foo(0) else bar(1))
''' foo and bar are executed once
foo executed
bar executed
1
'''
# Ternary Operator expression 1 equals to True
print(foo(2) if foo(2) else bar(3))
''' foo is executed twice!
foo executed
foo executed
2
'''
# Short-circuit evaluation second equals to True
print(foo(0) or bar(1) or blubb(2))
''' blubb is not executed
foo executed
bar executed
1
'''
# Short-circuit evaluation third equals to True
print(foo(0) or bar(0) or blubb(2))
'''
foo executed
bar executed
blubb executed
2
'''
# Short-circuit evaluation all equal to False
print(foo(0) or bar(0) or blubb(0))
''' Result is 0 (from blubb(0)) because no value equals to True
foo executed
bar executed
blubb executed
0
'''
PS:当然,一个短循环评估不是一个电路运营商,但经常在短循环足够的情况下使用电路,它具有更好的可读性,可以连锁。
推荐文章
- Pandas和NumPy+SciPy在Python中的区别是什么?
- 将列表转换为集合会改变元素的顺序
- 静态隐式运算符
- 如何在matplotlib更新一个情节
- TypeError: ` NoneType `对象在Python中不可迭代
- 如何在Vim注释掉一个Python代码块
- python标准库中的装饰符(特别是@deprecated)
- 如何从外部访问本地Django web服务器
- 删除字符串的最后3个字符
- 在python中执行no-op的标准方法是什么?
- 如何从生成器构建numpy数组?
- 逗号运算符是做什么的?
- 什么时候我应该(不)想要在我的代码中使用熊猫apply() ?
- 数据类vs类型。NamedTuple主要用例
- 如何从macOS完全卸载蟒蛇