Python for for语法:块代码vs单行生成器表达式 [英] Python `for` syntax: block code vs single line generator expressions
问题描述
我熟悉块代码上下文中循环的。例如:
for c inword:
print c
我刚才遇到了一些使用代替
的例子。它们不是以作为
语句的开始,而是在表达式的末尾标记它(而不涉及缩进的代码块)。例如:
sum(x * x在范围内(10))
任何人都可以指向一些文件,概述对的用法。我已经能够找到例子,但没有解释。我已经能够找到的文档的所有
描述了以前的使用(块代码示例)。我甚至不知道该怎么称呼这个用法,所以我很抱歉如果我的问题的标题不清楚。
在Python中指向 Generator
。看看: - $ / b
$ b
- 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
请参阅以下文档: - 生成器表达式
其中包含与您发布的示例完全相同的示例
从文档: -
生成器是创建迭代器的一个简单而强大的工具。他们
是像常规函数一样写的,但是只要他们想返回数据,就使用yield语句
。每当next()被调用时,
发生器会在其停止的地方恢复(它记住所有的数据值
以及哪个语句最后被执行)
生成器类似于 List Comprehension
,与方括号
一起使用括号
,但是它们的内存效率更高。它们不会同时返回完整的 list
结果,但它们会返回生成器对象。无论何时调用生成器
对象上的 next()
,生成器都使用 yield
返回下一个值。
上述代码的列表理解
看起来像: -
[x * x for x in range(10)]
您也可以添加条件来过滤掉结果。如果x%2!= 0]
数字
乘以2的范围在1到5的列表。
描述使用 yield
的 Generators
的例子可以是: -
def city_generator():
收益率(Konstanz)
收益率(Zurich)
收益率沙夫豪森)
收益率(斯图加特)
>>> x = city_generator()
>>> x.next()
康斯坦茨
>>> x.next()
苏黎世
>>> x.next()
沙夫豪森
>>> x.next()
斯图加特
>>> x.next()
Traceback(最近一次调用的最后一个):
在< module>文件中的< stdin>
StopIteration
所以,你会发现每次调用 next ()
执行 generator
中的下一个 yield()
。并在最后它抛出 StopIteration
。
I'm familiar with the for
loop in a block-code context. eg:
for c in "word":
print c
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))
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.
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
See the documentation: - Generator Expression
which contains exactly the same example you have posted
From the documentation: -
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)
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
for the above code would look like: -
[x * x for x in range(10)]
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]
This will return a list of numbers
multiplied by 2 in the range 1 to 5, if the number is not divisible by 2.
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
So, you see that, every call to next()
executes the next yield()
in generator
. and at the end it throws StopIteration
.
这篇关于Python for for语法:块代码vs单行生成器表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!