如何避免python中的StopIteration错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17317900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:52:33  来源:igfitidea点击:

How to avoid StopIteration Error in python

pythoniterationstopiteration

提问by Johnnerz

I have a line that is pulling in variables from multiple lists and I want it to avoid the StopIterationerror that comes up so that it can move onto the next line. At the moment I am using the break function, this avoids the StopIteration, but only gives me the first item in the list and it leaves a blank line after it, if i was to print it out.

我有一行从多个列表中提取变量,我希望它避免出现的StopIteration错误,以便它可以移动到下一行。目前我正在使用 break 函数,这避免了StopIteration, 但只给我列表中的第一项,如果我要打印出来,它会在它后面留下一个空行。

Here are two of my iterations that have the same problem.

这是我的两个具有相同问题的迭代。

def compose_line5(self, synset_offset, pointer_list):
    self.line5 = ''''''
    for item in pointer_list:
        self.line5 += '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.monnetproject.eu/lemon#has_ptr '''+pointer_list.next()+'''\n'''            
        break
    return self.line5

def compose_line6(self, pointer_list, synset_list): 
    self.line6 = ''''''
    for item in synset_list:
        self.line6 += '''http://www.example.org/lexicon#'''+pointer_list.next()+''' http://www.monnetproject.eu/lemon#pos '''+synset_list.next()+'''\n'''                      
        break
    return self.line6

This is the error I get without the break:

这是我没有休息时遇到的错误:

Traceback (most recent call last):
  File "wordnet.py", line 225, in <module>
    wordnet.line_for_loop(my_file)
  File "wordnet.py", line 62, in line_for_loop
    self.compose_line5(self.synset_offset, self.pointer_list)
  File "wordnet.py", line 186, in compose_line5
    self.line5 += '''http://www.example.org/lexicon#'''+self.synset_offset+''' http://www.monnetproject.eu/lemon#has_ptr '''+self.pointer_list.next()+'''\n'''
StopIteration

Is there a quick fix for this or do I have to catch exceptions for every method I use the iter() in?

对此是否有快​​速解决方案,或者我是否必须为使用 iter() 的每个方法捕获异常?

采纳答案by Thomas Vander Stichele

In compose_line5, instead of pointer_list.next(), use item- you are already iterating over pointer_list.

compose_line5, 而不是pointer_list.next(), use item- 你已经在迭代了pointer_list

For compose_line6, it seems you want to iterate over two lists at the same time. Use the top answer from Is there a better way to iterate over two lists, getting one element from each list for each iteration?(I'm assuming both lists are the same length)

对于compose_line6,您似乎想同时遍历两个列表。使用来自是否有更好的方法迭代两个列表的最佳答案,每次迭代从每个列表中获取一个元素?(我假设两个列表的长度相同)

Yes, the iterator protocol will raise StopIteration(not an error, just an exception signalling the end of the iteration) if you call .next()on it manually. The pythonic way to use it is to use it as a normal iterator (for example, looping over it) and not call .next()on it.

是的,StopIteration如果您.next()手动调用它,迭代器协议将引发(不是错误,只是一个异常信号迭代结束)。使用它的 Pythonic 方法是将它用作普通迭代器(例如,循环遍历它)而不是调用.next()它。

Your code has a few issues beyond that that you may want to look at - look at http://www.python.org/dev/peps/pep-0008/

您的代码还有一些您可能想要查看的问题 - 查看http://www.python.org/dev/peps/pep-0008/

For example, no need to use ''''''when ''suffices. Instead of doing +=, you may want to create a list then join in the end. Not sure why you store things in self if you're just returning them from the function.

例如,足够''''''时不需要使用''+=您可能想要创建一个列表,然后最后加入,而不是这样做。如果您只是从函数中返回它们,则不确定为什么要将它们存储在 self 中。