使用next()时停止迭代错误 [英] Stop Iteration error when using next()

查看:91
本文介绍了使用next()时停止迭代错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于python(3)中 next()的使用,我无法阐明自己.

I am not able to clarify my self over the use of next() in python(3).

我有一个数据:

chr pos ms01e_PI    ms01e_PG_al ms02g_PI    ms02g_PG_al ms03g_PI    ms03g_PG_al ms04h_PI    ms04h_PG_al
2   15881989    4   C|C 6   A|C 7   C|C 7   C|C
2   15882091    4   A|T 6   A|T 7   T|A 7   A|A
2   15882148    4   T|T 6   T|T 7   T|T 7   T|G

我的读法是这样的:

工作正常

c = csv.DictReader(io.StringIO(data), dialect=csv.excel_tab)
print(c)
print(list(c))

工作正常

c = csv.DictReader(io.StringIO(data), dialect=csv.excel_tab)
print(c)
keys = next(c)
print('keys:', keys)

但是,现在有问题.

c = csv.DictReader(io.StringIO(data), dialect=csv.excel_tab)
print(c)
print(list(c))
keys = next(c)
print('keys:', keys)

错误消息:

Traceback (most recent call last):
2   15882601    4   C|C 9   C|C 6   C|C 5   T|C

  File "/home/everestial007/Test03.py", line 24, in <module>
keys = next(c)
  File "/home/everestial007/anaconda3/lib/python3.5/csv.py", line 110, in __next__

    row = next(self.reader)

StopIteration

为什么在 print(list(c))后给出 StopIteration 后, print(keys)为什么?文档,但在这个特定示例上我不清楚.

Why does print(keys) after print(list(c)) gives StopIteration? I read the documentation but I am not clear on this particular example.

推荐答案

该错误与 print 语句无关.这是 keys = next(c)行.考虑一个更简单的示例,它再现了您的问题.

The error isn't with the print statement. It's with the keys = next(c) line. Consider a simpler example which reproduces your issue.

a = (i ** 2 for i in range(5))  

a           # `a` is a generator object
<generator object <genexpr> at 0x150e286c0>

list(a)     # calling `list` on `a` will exhaust the generator
[0, 1, 4, 9, 16]

next(a)     # calling `next` on an exhausted generator object raises `StopIteration`
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-2076-3f6e2eea332d> in <module>()
----> 1 next(a)

StopIteration: 

发生的情况是 c 是一个迭代器对象(与上面的 generator a 非常相似),并且打算对其进行迭代一次,直到筋疲力尽.在此对象上调用 list 将耗尽它,以便可以将元素收集到列表中.

What happens is that c is an iterator object (very similar to the generator a above), and is meant to be iterated over once until it is exhausted. Calling list on this object will exhaust it, so that the elements can be collected into a list.

对象耗尽后,将不再产生任何元素.在这一点上,生成器机制被设计为即使您已经用尽了它仍试图对其进行迭代,也会引发一个 StopIteration .诸如 for 循环之类的构造会侦听此错误并默默地吞下该错误,但是 next 会在引发异常后立即返回原始异常.

Once the object has been exhausted, it will not produce any more elements. At this point, the generator mechanism is designed to raise a StopIteration if you attempt to iterate over it even after it has been exhausted. Constructs such as for loops listen for this error, silently swallowing it, however, next returns the raw exception as soon as it has been raised.

这篇关于使用next()时停止迭代错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆