Python `for` 语法:块代码与单行生成器表达式 [英] Python `for` syntax: block code vs single line generator expressions
问题描述
我熟悉块代码上下文中的 for
循环.例如:
I'm familiar with the for
loop in a block-code context. eg:
for c in "word":
print c
我刚刚遇到了一些以不同方式使用 for
的示例.他们不是从 for
语句开始,而是在表达式的末尾标记它(并且不涉及缩进的代码块).例如:
I just came across some examples that use for
differently. Rather than beginning with the for
statement, they tag it at the end of an expression (and don't involve an indented code-block). eg:
sum(x*x for x in range(10))
谁能给我指点一些概述 for
用法的文档?我已经能够找到例子,但没有解释.我能找到的所有 for
文档都描述了以前的用法(块代码示例).我什至不知道如何称呼这种用途,所以如果我的问题标题不清楚,我深表歉意.
Can anyone point me to some documentation that outlines this use of for
? I've been able to find examples, but not explanations. All the for
documentation I've been able to find describes the previous use (block-code example). I'm not even sure what to call this use, so I apologize if my question's title is unclear.
推荐答案
你指的是 Python 中的 Generator
.看一看:-
What you are pointing to is Generator
in Python. Take a look at: -
- http://wiki.python.org/moin/Generators
- http://www.python.org/dev/peps/pep-0255/
- http://docs.python.org/whatsnew/2.5.html#pep-342-new-generator-features
请参阅文档: - Generator Expression
包含完全您发布的相同示例
See the documentation: - Generator Expression
which contains exactly the same example you have posted
来自文档:-
生成器是用于创建迭代器的简单而强大的工具.他们像常规函数一样编写,但使用 yield 语句每当他们想要返回数据时.每次调用 next() 时,生成器从停止的地方恢复(它记住所有数据值以及最后执行的是哪个语句)
Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called, the generator resumes where it left-off (it remembers all the data values and which statement was last executed)
生成器类似于您使用 方括号
而不是 方括号
的 List Comprehension
,但它们的内存效率更高.它们不会同时返回结果的完整list
,而是返回生成器对象.每当您在 generator
对象上调用 next()
时,生成器都会使用 yield
返回下一个值.
Generators are similar to List Comprehension
that you use with square brackets
instead of brackets
, but they are more memory efficient. They don't return the complete list
of result at the same time, but they return generator object. Whenever you invoke next()
on the generator
object, the generator uses yield
to return the next value.
List Comprehension
看起来像:-
[x * x for x in range(10)]
您还可以在 for 末尾添加条件来过滤结果.
You can also add conditions to filter out results at the end of the for.
[x * x for x in range(10) if x % 2 != 0]
如果数字不能被 2 整除,这将返回一个 numbers
乘以 2 的列表,范围为 1 到 5.
This will return a list of numbers
multiplied by 2 in the range 1 to 5, if the number is not divisible by 2.
描述 yield
使用的 Generators
示例可以是:-
An example of Generators
depicting the use of yield
can be: -
def city_generator():
yield("Konstanz")
yield("Zurich")
yield("Schaffhausen")
yield("Stuttgart")
>>> x = city_generator()
>>> x.next()
Konstanz
>>> x.next()
Zurich
>>> x.next()
Schaffhausen
>>> x.next()
Stuttgart
>>> x.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
所以,你看到了,对 next()
的每次调用都会执行 generator
中的下一个 yield()
.最后它抛出 StopIteration
.
So, you see that, every call to next()
executes the next yield()
in generator
. and at the end it throws StopIteration
.
这篇关于Python `for` 语法:块代码与单行生成器表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!