在Python中是否有特纳利条件操作器?
当前回答
一个提示比一个答案更多(我不需要重复显而易见的百度),但有时我用它作为一个单线缩短在这样的结构:
if conditionX:
print('yes')
else:
print('nah')
它变成:
print('yes') if conditionX else print('nah')
有些(很多)可能在它上被称为无神论(甚至,Ruby-ish :),但我个人觉得它更自然 - 也就是说,你会如何正常表达它,加上在大块代码中更有视觉吸引力。
其他回答
在Python中,Ternary Operator的合成是:
假如他是真實的,
使用这个合成,这里是我们如何使用Python的Ternary操作器重写上面的代码:
game_type = 'home'
shirt = 'white' if game_type == 'home' else 'green'
请注意,表达式可能是任何类型的表达式,包括函数呼叫,返回值,评估为真实或虚假。
如果否则版本可以写作如下:
sample_set="train" if "Train" in full_path else ("test" if "Test" in full_path else "validation")
其他答案是正确的谈论Python操作员. 我想补充提到一个场景,操作员经常使用,但有一个更好的字符。
假设我们希望使用选项_值与默认值,如果它没有设置:
run_algorithm(option_value if option_value is not None else 10)
或者,如果选项_值从未设置为虚假值(0,“”,等),
run_algorithm(option_value if option_value else 10)
但是,在这种情况下,一个越来越好的解决方案只是写作。
run_algorithm(option_value or 10)
作为Python Enhancement Proposal 308的一部分,2006年在Python中添加了一个条件表达的操作员,其形式不同于常见的?:操作员,它看起来如下:
<expression1> if <condition> else <expression2>
相当于:
if <condition>: <expression1> else: <expression2>
下面是一个例子:
result = x if a > b else y
可使用的另一个合成(可与 2.5 之前的版本兼容):
result = (lambda:y, lambda:x)[a > b]()
工人被精心评估。
另一种方式是通过索引一个<unk>(不符合大多数其他语言的条件运营商):
result = (y, x)[a > b]
或明确构建的词典:
result = {True: x, False: y}[a > b]
另一个(不太可靠),但更简单的方法是使用和和或运营商:
result = (a > b) and x or y
但是,如果 x 是虚假的,它就不会工作。
一个可能的工作岗位是创建 x 和 y 列表或列表如下:
result = ((a > b) and [x] or [y])[0]
或:
result = ((a > b) and (x,) or (y,))[0]
如果您正在使用字典,而不是使用一个温和的条件,您可以利用获得(关键,默认),例如:
shell = os.environ.get('SHELL', "/bin/sh")
来源:?:在维基百科的Python
特纳利条件运营商简单地允许在单一线测试一个条件,取代多线,如果它使代码紧凑。
合成:
假如他是真實的,
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中,年龄从出生日期开始
- 使用pip安装SciPy
- PHP中的双not(!!)操作符
- 在Python中,我应该如何测试变量是否为None, True或False
- 如何在Python中从毫秒创建datetime ?
- 如何解窝(爆炸)在一个熊猫数据帧列,成多行
- 如何使用pip安装opencv ?
- 在pip冻结命令的输出中“pkg-resources==0.0.0”是什么
- 格式y轴为百分比
- 熊猫连接问题:列重叠但没有指定后缀
- 为什么空字典在Python中是一个危险的默认值?
- 在Python中,冒号等于(:=)是什么意思?
- Python "SyntaxError:文件中的非ascii字符'\xe2' "
- 如何从psycopg2游标获得列名列表?
- Python中dict对象的联合