在Python中是否有特纳利条件操作器?
当前回答
<表达 1> 如果 <条件> 其他 <表达 2>
a = 1
b = 2
1 if a > b else -1
# Output is -1
1 if a > b else -1 if a < b else 0
# Output is -1
其他回答
连接多个运营商的方式:
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
a if condition else b
这使得短循环,因为当状态是真实的,只有A被评估,而B根本不被评估,但当状态是虚假的,只有B被评估,而A根本不被评估。
例如:
>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'
請注意,條件是表達,而不是表達,這意味著你無法在條件表達中使用表達,如通過,或與 = (或「增加」的表達,如 +=) 的表達:
>>> pass if False else pass
File "<stdin>", line 1
pass if False else pass
^
SyntaxError: invalid syntax
>>> # Python parses this as `x = (1 if False else y) = 2`
>>> # The `(1 if False else x)` part is actually valid, but
>>> # it can't be on the left-hand side of `=`.
>>> x = 1 if False else y = 2
File "<stdin>", line 1
SyntaxError: cannot assign to conditional expression
>>> # If we parenthesize it instead...
>>> (x = 1) if False else (y = 2)
File "<stdin>", line 1
(x = 1) if False else (y = 2)
^
SyntaxError: invalid syntax
(在 3.8 及以上, := “walrus” 操作员允许简单地将值定义为表达式,这与此同步,但请不要这样写代码,很快就会变得非常难以理解。
# Invalid syntax: we didn't specify what the value should be if the
# condition isn't met. It doesn't matter if we can verify that
# ahead of time.
a if True
但是,您可以使用条件表达式来分配变量如下:
x = a if True else b
例如,返回值:
# Of course we should just use the standard library `max`;
# this is just for demonstration purposes.
def my_max(a, b):
return a if a > b else b
请记住,它是由一些Pythonists为几种原因:
其他答案是正确的谈论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)
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
请注意,一些风格指南可以根据清晰度限制这种使用 - 它包装了很多意义到很少的合成。
有多种方式,最简单的是在“印刷”方法中使用状态。
你可以使用
print("Twenty" if number == 20 else "Not twenty")
相当于:
if number == 20:
print("Twenty")
else:
print("Not twenty")
因此,可以打印超过两份声明,例如:
if number == 20:
print("Twenty")
elif number < 20:
print("Lesser")
elif 30 > number > 20:
print("Between")
else:
print("Greater")
可以写作如:
print("Twenty" if number == 20 else "Lesser" if number < 20 else "Between" if 30 > number > 20 else "Greater")
推荐文章
- 在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对象的联合