在Python中是否有特纳利条件操作器?
当前回答
你可能经常发现
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
其他回答
一个提示比一个答案更多(我不需要重复显而易见的百度),但有时我用它作为一个单线缩短在这样的结构:
if conditionX:
print('yes')
else:
print('nah')
它变成:
print('yes') if conditionX else print('nah')
有些(很多)可能在它上被称为无神论(甚至,Ruby-ish :),但我个人觉得它更自然 - 也就是说,你会如何正常表达它,加上在大块代码中更有视觉吸引力。
我发现默认的Python合成val = a if cond other b cumbersome,所以有时我这样做:
iif = lambda (cond, a, b): a if cond else b
# So I can then use it like:
val = iif(cond, a, b)
当然,它有一个缺点,总是评估双方(a和b),但合成对我来说更清楚。
正如我已经回答过的那样,是的,在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:当然,一个短循环评估不是一个电路运营商,但经常在短循环足够的情况下使用电路,它具有更好的可读性,可以连锁。
test: or_test ['if' or_test 'else' test] | lambdef
兴趣的一部分是:
or_test ['if' or_test 'else' test]
expression1 if expression2 else expression3
expression3 将被轻松评估(即只有在 boolean 背景下, expression2 是虚假的),并且由于重复的定义,你可以无限地链接它们(也许它可能被认为是坏风格)。
expression1 if expression2 else expression3 if expression4 else expression5 # and so on
人们学习列表的理解和发明表达可能认为这是一个难以学习的教训 - 以下不会工作,因为Python预期另一个第三个表达:
[expression1 if expression2 for element in iterable]
# ^-- need an else here
[expression1 for element in iterable if expression2]
expression1 if expression1 else expression2
expression1 or expression2
请注意,一些风格指南可以根据清晰度限制这种使用 - 它包装了很多意义到很少的合成。
你可以把它分成一个<unk>子:
(falseValue, trueValue)[test]
测试需要返回真实或虚假. 它可能更安全地总是执行它如下:
(falseValue, trueValue)[test == True]
或者您可以使用内置的 bool() 来确保 Boolean 值:
(falseValue, trueValue)[bool(<expression>)]
推荐文章
- Python中dict对象的联合
- 如何有效地比较两个无序列表(不是集合)?
- 如何在交互式Python中查看整个命令历史?
- 如何显示有两个小数点后的浮点数?
- 如何用OpenCV2.0和Python2.6调整图像大小
- 在每个列表元素上调用int()函数?
- 当使用代码存储库时,如何引用资源的相对路径
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 在matplotlib中将y轴标签添加到次要y轴