不知道为什么我收到 StopIteration 错误

泰勒·邓恩

我正在编写一个从文件接收输入的程序,每行可能包含“ATG”或“GTG”,我很确定我已经完成了我想要做的所有事情。这是我第一次在 python 中使用生成器,在研究了这个问题之后,我仍然不知道为什么我会停止迭代。为此,我的生成器必须生成一个元组,其中包含在每个字符串中找到的 ATG 或 GTG 的起始位置。

import sys

import p3mod


gen = p3mod.find_start_positions()
gen.send(None)   # prime the generator

with open(sys.argv[1]) as f:
    for line in f:
        (seqid,seq) = line.strip().lower().split()
        slocs = gen.send(seq)
        print(seqid,slocs,"\n")

gen.close()  ## added to be more official

这是发电机

def find_start_positions (DNAstr = ""):

    DNAstr = DNAstr.upper()

    retVal = ()
    x = 0
    loc = -1

    locations = []

    while (x + 3) < len(DNAstr):

        if (DNAst[x:x+3] is "ATG" or DNAstr[x:x+3] is "GTG" ):
            loc = x

        if loc is not -1:
            locations.append(loc)

        loc = -1

    yield (tuple(locations))

这是错误:

Traceback (most recent call last):
  File "p3rmb.py", line 12, in <module>
    slocs = gen.send(seq)
StopIteration
杰梅·托西·内托

您制作了一个一次性返回所有数据的生成器。您应该在每次迭代中产生数据。这段代码可能并不完美,但它可能会解决您的部分问题:

def find_start_positions (DNAstr = ""):
    DNAstr = DNAstr.upper()

    x = 0
    loc = -1

    while x + 3 < len(DNAstr):
        if DNAst[x:x+3] == "ATG" or DNAstr[x:x+3] == "GTG" :
            loc = x

        if loc is not -1:
            yield loc

        loc = -1

StopIteration 不是错误。这是生成器发出信号表示它已耗尽所有数据的方式。您只需要“尝试除外”它或在已经为您执行此操作的 forloop 中使用您的生成器。尽管它们并没有那么复杂,但习惯这些“奇怪”的错误可能需要一些时间。;)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我不知道为什么我收到以下错误“错误:元素类型无效:”

我收到这个 Intent 的错误,我不知道为什么

我不知道为什么我在 jsp 文件中收到这个简单代码的错误

Netsuite:我不知道为什么我的 query.load 收到此错误

我不断收到此错误消息,但我不知道为什么

我收到 mysqli 错误 1064 但我不知道为什么

我在 CSS 中不断收到此错误,但我不知道为什么

不知道为什么我会收到此错误“SyntaxError: invalid token”?

我不知道为什么会收到“ Addressof”错误?

SMTP 错误,我不知道为什么

Dockerfile 错误的目录。我不知道为什么

这是我的最终代码。我最终多次收到错误消息,但我不知道为什么

我在 Delphi 中收到“未声明的标识符”错误,我不知道为什么?

收到int错误,但我不知道为什么。我的文件没有多余的空格

我不知道为什么,但我收到错误:“AttributeError: 'super' 对象没有属性 '__getattr__'”

复制代码后,我在 Unity 中收到错误 Unexpected symbol 'void',我不知道为什么

不知道为什么我在使用 Vue 时收到“错误 'Navbar' 已定义但从未使用过”

不知道为什么我会收到此错误:方法Illuminate \ View \ View :: paginate不存在?

在RecordSet上收到错误3001,不知道为什么

不知道为什么会收到错误:<class>的正向声明

收到MySQL语法错误,不知道为什么

dplyr::gather - 收到错误消息但不知道为什么

为什么我收到这个错误?似乎我在 s1.grade_level 中有错误,但我不知道为什么

我不断得到细分错误:11错误,我不知道为什么... c ++

我不知道为什么我的代码是错误的?那是什么错呢?

我的代码给了我一个元组错误,我不知道为什么

我收到此错误,“错误:'{'令牌之前的预期表达式”。我不知道为什么?语法对我来说很好

我不知道为什么我得到这个文件,但是仍然显示错误

我的min(list)函数产生错误的输出,我不知道为什么