在Python中是否有特纳利条件操作器?
当前回答
你可以这样做:
[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];
其他回答
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
请注意,一些风格指南可以根据清晰度限制这种使用 - 它包装了很多意义到很少的合成。
对于 Python 2.5 或更高版本,有一个具体的合成:
[on_true] if [cond] else [on_false]
在更老的Pythons中,一个特纳运营商没有实施,但可以模拟它。
cond and on_true or on_false
雖然有一個可能的問題,即如果 cond 評估到 True 和 on_true 評估到 False 然後 on_false 返回而不是 on_true. 如果你想要這種行為的方法是 OK,否則使用此方法:
{True: on_true, False: on_false}[cond is True] # is True, not == True
可以包装的:
def q(cond, on_true, on_false)
return {True: on_true, False: on_false}[cond is True]
以此方式使用:
q(cond, on_true, on_false)
它与所有 Python 版本兼容。
连接多个运营商的方式:
f = lambda x,y: 'greater' if x > y else 'less' if y > x else 'equal'
array = [(0,0),(0,1),(1,0),(1,1)]
for a in array:
x, y = a[0], a[1]
print(f(x,y))
# Output is:
# equal,
# less,
# greater,
# equal
特纳利条件运营商简单地允许在单一线测试一个条件,取代多线,如果它使代码紧凑。
合成:
假如他是真實的,
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
在 2.5 之前的版本中,有以下技巧:
[expression] and [on_true] or [on_false]
它可以提供错误的结果,当 on_true 具有虚假的 Boolean 值。
雖然它有評估表達的好處留在右邊,這在我看來更清楚。
是否有“C”的“?”:“特纳利运营商的同等值?
推荐文章
- Pandas和NumPy+SciPy在Python中的区别是什么?
- 将列表转换为集合会改变元素的顺序
- 静态隐式运算符
- 如何在matplotlib更新一个情节
- TypeError: ` NoneType `对象在Python中不可迭代
- 如何在Vim注释掉一个Python代码块
- python标准库中的装饰符(特别是@deprecated)
- 如何从外部访问本地Django web服务器
- 删除字符串的最后3个字符
- 在python中执行no-op的标准方法是什么?
- 如何从生成器构建numpy数组?
- 逗号运算符是做什么的?
- 什么时候我应该(不)想要在我的代码中使用熊猫apply() ?
- 数据类vs类型。NamedTuple主要用例
- 如何从macOS完全卸载蟒蛇