在Python中是否有特纳利条件操作器?
当前回答
作为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
很常见需要根据一个条件将一个值或另一个值分配给一个变量。
>>> li1 = None
>>> li2 = [1, 2, 3]
>>>
>>> if li1:
... a = li1
... else:
... a = li2
...
>>> a
[1, 2, 3]
下面是<unk>形状,但这不是最简短的方式 - 见最后一个例子。
>>> a = li1 if li1 else li2
>>>
>>> a
[1, 2, 3]
>>>
>>> a = li1 or li2
>>>
>>> a
[1, 2, 3]
>>>
上述工作,因为 li1 是 无 和 翻译者 将其视为 逻辑表达中的虚假 然后 翻译者 移动并评估第二个表达,这不是 无 和 它不是一个空白的列表 - 因此,它被归咎于 a。
这也适用于空白列表,例如,如果你想分配一个任何列表都有项目。
>>> li1 = []
>>> li2 = [1, 2, 3]
>>>
>>> a = li1 or li2
>>>
>>> a
[1, 2, 3]
>>>
>>> s1 = ''
>>> s2 = 'hello world'
>>>
>>> a = s1 or s2
>>>
>>> a
'hello world'
>>>
我明白,有些人可能会说这不是一个很好的风格选择,因为它依赖于机械,不立即显而易见的所有开发人员. 我个人不同意这个观点. Python 是一个合成丰富的语言,有很多愚蠢的技巧,不立即显而易见的破坏者。
是的,Python有一个特纳运营商,这里是合成和一个示例代码来展示相同的:)
#[On true] if [expression] else[On false]
# if the expression evaluates to true then it will pass On true otherwise On false
a = input("Enter the First Number ")
b = input("Enter the Second Number ")
print("A is Bigger") if a>b else print("B is Bigger")
是的,它有,但它与C类似的编程语言不同(这是条件? value_if_true : value_if_false
在 Python 中,它如下: value_if_true 如果另一个条件值_if_false
例如: even_or_odd = “even” 如果 x % 2 == 0 其他 “odd”
正如我已经回答过的那样,是的,在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:当然,一个短循环评估不是一个电路运营商,但经常在短循环足够的情况下使用电路,它具有更好的可读性,可以连锁。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录