我正在写一个程序,从一个文件接受输入,每一行可能包含“ATG”或“GTG”,我很确定我做了一切正确的事情,尽可能我正在尝试做什么。 IT是我第一次在Python中使用一个生成器,并在研究这个问题后,我仍然不知道为什么我要停止迭代。为此,我的生成器必须产生一个元组,其中每个字符串中都有ATG或GTG的起始位置。不知道为什么我得到StopIteration错误
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
有没有打印出来,显示每行回溯? – JacobIRR
Traceback(最近一次调用最后一个):文件“p3rmb.py”,第12行,在 slocs = gen.send(seq) StopIteration –
如果您调用'send',需要将yield分配给一个值。 –