我需要一个 2D 循环,其中第一个循环使用迭代器,第二个循环使用生成器,但是这个简单的函数无法工作,谁能帮忙检查一下?
I need a 2D loop of which the first loop uses an iterator and the second uses a generator, but this simple function failed to work, can anyone help to check?
def alphabet(begin, end):
for number in xrange(ord(begin), ord(end)+1):
yield chr(number)
def test(a, b):
for i in a:
for j in b:
print i, j
test(xrange(8, 10), alphabet('A', 'C'))
The result shows:
>>> 8 A
>>> 8 B
>>> 8 c
不知道为什么?如果有人可以提供帮助,请提前致谢.
don't know why? thanks in advance if any one can help.
既然你要求澄清,我就多说一点;但真的 Ignacio 的回答总结得很好:您只能迭代生成器一次.您示例中的代码尝试对其进行三次迭代,a 中的每个值一次.
Since you've asked for clarification, I'll say a bit more; but really Ignacio's answer sums it up pretty well: you can only iterate over a generator once. The code in your example tries to iterate over it three times, once for each value in a.
要明白我的意思,请考虑这个简单的例子:
To see what I mean, consider this simplistic example:
>>> def mygen(x):
... i = 0
... while i < x:
... yield i
... i += 1
...
>>> mg = mygen(4)
>>> list(mg)
[0, 1, 2, 3]
>>> list(mg)
[]
当 mygen 被调用时,它会创建一个可以只迭代一次的对象.当您尝试再次对其进行迭代时,您会得到一个空的可迭代对象.
When mygen is called, it creates an object which can be iterated over exactly once. When you try to iterate over it again, you get an empty iterable.
这意味着你必须重新调用 mygen,每次你想要迭代它`.所以换句话说(使用相当冗长的风格)......
This means you have to call mygen anew, every time you want to iterate over it`. So in other words (using a rather verbose style)...
>>> def make_n_lists(gen, gen_args, n):
... list_of_lists = []
... for _ in range(n):
... list_of_lists.append(list(gen(*gen_args)))
... return list_of_lists
...
>>> make_n_lists(mygen, (3,), 3)
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
如果您想将参数绑定到生成器并将其作为无参数函数传递,您可以这样做(使用更简洁的样式):
If you wanted to bind your arguments to your generator and pass that as an argumentless function, you could do this (using a more terse style):
>>> def make_n_lists(gen_func, n):
... return [list(gen_func()) for _ in range(n)]
...
>>> make_n_lists(lambda: mygen(3), 3)
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
lambda 只是定义了一个匿名函数;以上与此相同:
The lambda just defines an anonymous function; the above is identical to this:
>>> def call_mygen_with_3():
... return mygen(3)
...
>>> make_n_lists(call_mygen_with_3, 3)
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
这篇关于使用生成器和迭代器时 Python 多循环失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
使用 python 解析非常大的 xml 文件时出现问题Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件时出现问题)
使用 Python 2 在 XML 中按属性查找所有节点Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按属性查找所有节点)
Python - 如何解析 xml 响应并将元素值存储在变量中Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 响应并将元素值存储在变量中?)
如何在 Python 中获取 XML 标记值How to get XML tag value in Python(如何在 Python 中获取 XML 标记值)
如何使用 ElementTree 正确解析 utf-8 xml?How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正确解析 utf-8 xml?)
将 XML 从 URL 解析为 python 对象Parse XML from URL into python object(将 XML 从 URL 解析为 python 对象)