Python 在 for 循环中使用三元运算符
Python use ternary operator with for loop
我正在尝试改造这个
def opt_config(opts):
for x, x2 in opts:
if x == "--config":
return x2
return str()
使用像
这样的三元运算符
return x2 if x == "--config" for x, x2 in opts else str()
不过好像不行。我该如何更正它?
您必须为此使用生成器表达式:
return next((x2 for x, x2 in opts if x == "--config"), str())
表达式(x2 for x, x2 in opts if x == 1)
是一个生成器:它
可用于迭代一组元素。例如:
opts = [(1, 2), (1, 3), (2, 4)]
gen = (x2 for x, x2 in opts if x == 1)
for x in gen:
print(x) # print 2 and 3
next
returns 生成器的下一个元素(或者第一个,如果你
目前还没有生成任何元素)。
如果生成器不包含任何下一个元素,则第二个
next
的(可选)参数是 returned。否则会引发异常。例如:
opts = [(1, 2), (2, 3)]
gen = (x2 for x, x2 in opts if x == 4)
x = next(gen) # raise StopIteration
gen = (x2 for x, x2 in opts if x == 4)
x = next(gen, 5)
print(x) # print 5
总而言之,return next((x2 for x, x2 in opts if x == "--config"), str())
等同于您的 for 循环:使用您的生成器,您获取 opts
的第一个元素 x, x2
,使得 x == "--config"
和 return x2
;如果没有这样的元素你 return str()
这里有一些片段。
str()
的回退 return 将始终给出 ''
,因为您要求它的字符串值几乎为空。如果那是你想要的,你也可以 return ''
代替。
除非你所有的选择都是 2 元组(或其他每个有 2 个元素的迭代),否则任何单例选择都会在 for x, x2 in opts
处失败。猜测,您更有可能向此函数提供一个字符串列表,例如
opt_config(['--config', 'elderberries', 'castle']) -> 'elderberries'
opt_config(['grail', 'swallow', '--config', 'rabbit']) -> 'rabbit'
opt_config(['parrot', 'cheese', 'Olympics']) -> ''
同样,推测你想要什么,看来你想要第一个配置选项(而且只有第一个),或者 ''
如果 '--config'
从未出现。
如果我对您的意图的解释是正确的,那么您可能想要 'non-ternary' 版本的代码:
def opt_config(opts):
if '--config' in opts:
ndx = opts.index('--config')
if ndx < len(opts):
return opts[ndx + 1]
return ''
opt_config(['--config', 'elderberries', 'castle'])
'elderberries'
opt_config(['grail', 'swallow', '--config', 'rabbit'])
'rabbit'
opt_config(['parrot', 'cheese', 'Olympics'])
''
如果您想压缩该代码,您可以使用您询问的三元运算符将它一直压缩到 1-liner:
opt_config2 = lambda opts: opts[opts.index('--config') + 1] if '--config' in opts[:-1] else ''
opt_config2(['--config', 'elderberries', 'castle'])
'elderberries'
opt_config2(['grail', 'swallow', '--config', 'rabbit'])
'rabbit'
opt_config2(['parrot', 'cheese', 'Olympics'])
''
使用三元运算符 x if y else z
,首先计算 if y
部分,因此 python 有效地仅计算 x
和 z
选项之一根据需要。
我正在尝试改造这个
def opt_config(opts):
for x, x2 in opts:
if x == "--config":
return x2
return str()
使用像
这样的三元运算符return x2 if x == "--config" for x, x2 in opts else str()
不过好像不行。我该如何更正它?
您必须为此使用生成器表达式:
return next((x2 for x, x2 in opts if x == "--config"), str())
表达式(x2 for x, x2 in opts if x == 1)
是一个生成器:它
可用于迭代一组元素。例如:
opts = [(1, 2), (1, 3), (2, 4)]
gen = (x2 for x, x2 in opts if x == 1)
for x in gen:
print(x) # print 2 and 3
next
returns 生成器的下一个元素(或者第一个,如果你
目前还没有生成任何元素)。
如果生成器不包含任何下一个元素,则第二个
next
的(可选)参数是 returned。否则会引发异常。例如:
opts = [(1, 2), (2, 3)]
gen = (x2 for x, x2 in opts if x == 4)
x = next(gen) # raise StopIteration
gen = (x2 for x, x2 in opts if x == 4)
x = next(gen, 5)
print(x) # print 5
总而言之,return next((x2 for x, x2 in opts if x == "--config"), str())
等同于您的 for 循环:使用您的生成器,您获取 opts
的第一个元素 x, x2
,使得 x == "--config"
和 return x2
;如果没有这样的元素你 return str()
这里有一些片段。
str()
的回退 return 将始终给出 ''
,因为您要求它的字符串值几乎为空。如果那是你想要的,你也可以 return ''
代替。
除非你所有的选择都是 2 元组(或其他每个有 2 个元素的迭代),否则任何单例选择都会在 for x, x2 in opts
处失败。猜测,您更有可能向此函数提供一个字符串列表,例如
opt_config(['--config', 'elderberries', 'castle']) -> 'elderberries'
opt_config(['grail', 'swallow', '--config', 'rabbit']) -> 'rabbit'
opt_config(['parrot', 'cheese', 'Olympics']) -> ''
同样,推测你想要什么,看来你想要第一个配置选项(而且只有第一个),或者 ''
如果 '--config'
从未出现。
如果我对您的意图的解释是正确的,那么您可能想要 'non-ternary' 版本的代码:
def opt_config(opts):
if '--config' in opts:
ndx = opts.index('--config')
if ndx < len(opts):
return opts[ndx + 1]
return ''
opt_config(['--config', 'elderberries', 'castle'])
'elderberries'
opt_config(['grail', 'swallow', '--config', 'rabbit'])
'rabbit'
opt_config(['parrot', 'cheese', 'Olympics'])
''
如果您想压缩该代码,您可以使用您询问的三元运算符将它一直压缩到 1-liner:
opt_config2 = lambda opts: opts[opts.index('--config') + 1] if '--config' in opts[:-1] else ''
opt_config2(['--config', 'elderberries', 'castle'])
'elderberries'
opt_config2(['grail', 'swallow', '--config', 'rabbit'])
'rabbit'
opt_config2(['parrot', 'cheese', 'Olympics'])
''
使用三元运算符 x if y else z
,首先计算 if y
部分,因此 python 有效地仅计算 x
和 z
选项之一根据需要。