gpt4 book ai didi

python - 不断收到 IndexError 并且不确定为什么在 Python 中

转载 作者:行者123 更新时间:2023-12-01 04:43:56 26 4
gpt4 key购买 nike

我是 Python 新手,并且真正进行过一般编程,并且正在通过一个名为 rosalind.info 的网站学习 Python,该网站旨在通过解决问题进行教学。

Here is the problem, wherein you're asked to calculate the percentage of guanine and thymine to the string of DNA given to for each ID, then return the ID of the sample with the greatest percentage.

我正在处理页面上的示例问题,但遇到了一些困难。我知道我的代码可能确实效率低下且麻烦,但我认为这对于那些刚接触编程的人来说是可以预料的。

无论如何,这是我的代码。

gc = open("rosalind_gcsamp.txt","r")
biz = gc.readlines()
i = 0
gcc = 0
d = {}
for i in xrange(biz.__len__()):
if biz[i].startswith(">"):
biz[i] = biz[i].replace("\n","")
biz[i+1] = biz[i+1].replace("\n","") + biz[i+2].replace("\n","")
del biz[i+2]

我在这里想要完成的是,给定如下输入:

>Rosalind_6404CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCCTCCCACTAATAATTCTGAGG

Break what's given into a list based on the lines and concatenate the two lines of DNA like so:

['>Rosalind_6404', 'CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCCTCCCACTAATAATTCTGAGG', 'TCCCACTAATAATTCTGAGG\n']

And delete the entry two indices after the ID, which is >Rosalind. What I do with it later I still need to figure out.

However, I keep getting an index error and can't, for the life of me, figure out why. I'm sure it's a trivial reason, I just need some help.

I've even attempted the following to limited success:

for i in xrange(biz.__len__()):
if biz[i].startswith(">"):
biz[i] = biz[i].replace("\n","")
biz[i+1] = biz[i+1].replace("\n","") + biz[i+2].replace("\n","")
elif biz[i].startswith("A" or "C" or "G" or "T") and biz[i+1].startswith(">"):
del biz[i]

这仍然给我一个索引错误,但至少给了我我想要的商业值(value)。

提前致谢。

最佳答案

使用 itertools.groupby 使用以 > 开头的行作为键和分隔符非常容易:

from itertools import groupby
with open("rosalind_gcsamp.txt","r") as gc:
# group elements using lines that start with ">" as the delimiter
groups = groupby(gc, key=lambda x: not x.startswith(">"))
d = {}
for k,v in groups:
# if k is False we a non match to our not x.startswith(">")
# so use the value v as the key and call next on the grouper object
# to get the next value
if not k:
key, val = list(v)[0].rstrip(), "".join(map(str.rstrip,next(groups)[1],""))
d[key] = val

print(d)
{'>Rosalind_0808': 'CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGACTGGGAACCTGCGGGCAGTAGGTGGAAT', '>Rosalind_5959': 'CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCTATATCCATTTGTCAGCAGACACGC', '>Rosalind_6404': 'CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCCTCCCACTAATAATTCTGAGG'}

如果您需要订单,请使用 collections.OrderedDict 代替 d。

关于python - 不断收到 IndexError 并且不确定为什么在 Python 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com