在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
其他回答
你可以这样做:
[condition] and [expression_1] or [expression_2];
例子:
print(number%2 and "odd" or "even")
注: 0, 没有, 虚假, 虚假, 和 emptyString 被评为 虚假。
除0以外的任何数据都被评为真实。
如果我们“和”与0(零)的东西,结果将永远是虚假的。
0 and exp
表达式 exp 将不会被评估,因为“和”与 0 将始终被评估为零,并且不需要评估表达式。
在
1 or exp
exp 表达式将不会被评估,因为“或”与 1 将永远是 1. 所以不会打扰评估 exp 表达式,因为结果将是 1 无论如何(计算机优化方法)。
但是,如果
True and exp1 or exp2
同样在
False and exp1 or exp2
exp1 表达式不会被评估,因为 False 相当于 0 写作,并且与 0 做“和”将是 0 本身,但在 exp1 使用“或”之后,它将评估 exp2 表达式后“或”。
[condition] and ([expression_1] or 1) or [expression_2];
在Python中,Ternary Operator的合成是:
假如他是真實的,
使用这个合成,这里是我们如何使用Python的Ternary操作器重写上面的代码:
game_type = 'home'
shirt = 'white' if game_type == 'home' else 'green'
请注意,表达式可能是任何类型的表达式,包括函数呼叫,返回值,评估为真实或虚假。
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
请注意,一些风格指南可以根据清晰度限制这种使用 - 它包装了很多意义到很少的合成。
特纳利条件运营商简单地允许在单一线测试一个条件,取代多线,如果它使代码紧凑。
合成:
假如他是真實的,
1、使用Ternary Operator的简单方法:
# Program to demonstrate conditional operator
a, b = 10, 20
# Copy value of a in min if a < b else copy b
min = a if a < b else b
print(min) # Output: 10
2、直接使用Tuples、词典和Lambda的方法:
# Python program to demonstrate ternary operator
a, b = 10, 20
# Use tuple for selecting an item
print( (b, a) [a < b] )
# Use Dictionary for selecting an item
print({True: a, False: b} [a < b])
# lambda is more efficient than above two methods
# because in lambda we are assure that
# only one expression will be evaluated unlike in
# tuple and Dictionary
print((lambda: b, lambda: a)[a < b]()) # in output you should see three 10
3、管道运营商可以写作如下:
# Python program to demonstrate nested ternary operator
a, b = 10, 20
print ("Both a and b are equal" if a == b else "a is greater than b"
if a > b else "b is greater than a")
上面的方法可以写作如:
# Python program to demonstrate nested ternary operator
a, b = 10, 20
if a != b:
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
else:
print("Both a and b are equal")
# Output: b is greater than a
我发现默认的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),但合成对我来说更清楚。
推荐文章
- 将列表转换为集合会改变元素的顺序
- 静态隐式运算符
- 如何在matplotlib更新一个情节
- TypeError: ` NoneType `对象在Python中不可迭代
- 如何在Vim注释掉一个Python代码块
- python标准库中的装饰符(特别是@deprecated)
- 如何从外部访问本地Django web服务器
- 删除字符串的最后3个字符
- 在python中执行no-op的标准方法是什么?
- 如何从生成器构建numpy数组?
- 逗号运算符是做什么的?
- 什么时候我应该(不)想要在我的代码中使用熊猫apply() ?
- 数据类vs类型。NamedTuple主要用例
- 如何从macOS完全卸载蟒蛇
- 是否有可能键入提示一个lambda函数?