是否可以使用“else”?在列表理解中?
这是我试图转换为列表理解的代码:
table = ''
for index in xrange(256):
if index in ords_to_keep:
table += chr(index)
else:
table += replace_with
有没有办法将 else 语句添加到此理解中?
table = ''.join(chr(index) for index in xrange(15) if index in ords_to_keep)
Here is the code I was trying to turn into a list comprehension:
table = ''
for index in xrange(256):
if index in ords_to_keep:
table += chr(index)
else:
table += replace_with
Is there a way to add the else statement to this comprehension?
table = ''.join(chr(index) for index in xrange(15) if index in ords_to_keep)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
语法
a if b else c
是 Python 中的三元运算符,如果条件b
为真,则计算结果为a
- 否则,计算结果为c
.它可以用在理解语句中:所以对于你的例子,
The syntax
a if b else c
is a ternary operator in Python that evaluates toa
if the conditionb
is true - otherwise, it evaluates toc
. It can be used in comprehension statements:So for your example,
要在 Python 编程中的列表推导中使用
else
,您可以尝试以下代码片段。这将解决您的问题,该代码片段在 python 2.7 和 python 3.5 上进行了测试。To use the
else
in list comprehensions in python programming you can try out the below snippet. This would resolve your problem, the snippet is tested on python 2.7 and python 3.5.如果您想要一个
else
,您不想过滤列表理解,您希望它迭代每个值。您可以使用true-value if cond else false-value
作为语句,并从末尾删除过滤器:If you want an
else
you don't want to filter the list comprehension, you want it to iterate over every value. You can usetrue-value if cond else false-value
as the statement instead, and remove the filter from the end:是,
else
可以在Python中使用列表
带有 的理解 条件表达式(“三元运算符”):这里的括号“()”只是为了强调条件表达式,不一定是必需的(运算符优先级)。
此外,可以嵌套多个表达式,从而导致更多的 else 和难以阅读的代码:
在相关说明中,推导式还可以包含自己的 if 条件最后:
条件s?是的,多个
if
是可能的,实际上也可以有多个for
:(单个下划线
_
是一个有效的变量名称(标识符),此处使用只是为了表明它实际上并未使用它在交互中具有特殊含义。模式)将其用于附加条件表达式是可能的,但没有实际用途:
推导式也可以嵌套来创建“多维”列表(“数组”):
最后但并非最不重要的一点是,推导式不是仅限于创建
list
,即else
和if
也可以在set
理解:和
字典
理解:相同的语法也用于 < a href="https://docs.python.org/3/reference/expressions.html#generator-expressions" rel="noreferrer">生成器表达式:
可用于创建元组 (没有元组理解)。
Further reading:
Yes,
else
can be used in Python inside alist
comprehension with a Conditional Expression ("ternary operator"):Here, the parentheses "()" are just to emphasize the conditional expression, they are not necessarily required (Operator precedence).
Additionaly, several expressions can be nested, resulting in more
else
s and harder to read code:On a related note, a comprehension can also contain its own
if
condition(s) at the end:Conditions? Yes, multiple
if
s are possible, and actually multiplefor
s, too:(The single underscore
_
is a valid variable name (identifier) in Python, used here just to show it's not actually used. It has a special meaning in interactive mode)Using this for an additional conditional expression is possible, but of no real use:
Comprehensions can also be nested to create "multi-dimensional" lists ("arrays"):
Last but not least, a comprehension is not limited to creating a
list
, i.e.else
andif
can also be used the same way in aset
comprehension:and a
dictionary
comprehension:The same syntax is also used for Generator Expressions:
which can be used to create a
tuple
(there is no tuple comprehension).Further reading:
或许。列表推导式本质上计算效率并不高。它仍然以线性时间运行。
从我个人的经验来看:
通过用上面的 for 循环/列表附加类型结构替换列表推导式(特别是嵌套推导式),我在处理大型数据集时显着减少了计算时间。在此应用程序中,我怀疑您会注意到差异。
Maybe. List comprehensions are not inherently computationally efficient. It is still running in linear time.
From my personal experience:
I have significantly reduced computation time when dealing with large data sets by replacing list comprehensions (specifically nested ones) with for-loop/list-appending type structures you have above. In this application I doubt you will notice a difference.
很好的答案,但只是想提一个问题,即“pass”关键字在列表理解的 if/else 部分中不起作用(如上面提到的示例中发布的)。
这是在 python 3.4 上尝试和测试的。
错误如下:
因此,尽量避免列表推导中的 pass-es
Great answers, but just wanted to mention a gotcha that "pass" keyword will not work in the if/else part of the list-comprehension (as posted in the examples mentioned above).
This is tried and tested on python 3.4.
Error is as below:
So, try to avoid pass-es in list comprehensions