IndexError:列表索引超出范围,不确定为什么

时间:2015-04-27 07:08:52

标签: python attributeerror indexoutofrangeexception

我希望程序要做的是获取与某个条形码相关的序列并执行定义的功能(序列的平均长度和标准差,减去由相同条形码识别的条形码和非相关txt)。我已经写了类似的东西并基于类似的程序,但我不断得到一个indexerror。这个想法是所有带有第一个条形码的序列都将被处理为barcodeCounter = 0,第二个序列将被处理为barcodeCounter = 1,等等。希望这是足够的信息,对不起,如果它是凌乱的。

输入:

import sys
import math

def avsterr(x):
        ave = sum(x)/len(x)
        ssq = 0.0
        for y in x:
                ssq += (y-ave)*(y-ave)
        var = ssq / (len(x)-1)
        sdev = math.sqrt(var)
        stderr = sdev / math.sqrt(len(x))

        return (ave,stderr)

barcode = sys.argv[1]
sequence = sys.argv[2]
lengths = []
toprocess = []
b = open(barcode,"r")
barcodeCounter = 0
for barcode in b:
        barcodeCounter = barcodeCounter + 1
        barcode = barcode.strip()
        print "barcode: %s" %  barcode
        handle = open(sequence, "r")
        for line in handle:
                print line
                seq = line.split(' ',1)[-1].strip()
                print "seq: %s" % seq
                potential_barcode = seq[0:len(barcode)]
                print "something"
                if potential_barcode == barcode:
                        print "Checking sequences"
                        outseq = seq.replace(potential_barcode, "", 1)
                        outseq_length = [len(outseq)]
#                       toprocess.append("")
#                       toprocess[barcodeCounter] += outseq.strip
                        toprocess[barcodeCounter].extend(outseq.strip)   #IndexError/line40
#                       toprocess[barcodeCounter] = toprocess[barcodeCounter] + outseq.strip
                        print "outseq: %s" % outseq
                        print "Barcodes to be processed: %s" % toprocess[barcodeCounter]
                        print "BC: %i" % barcodeCounter
        handle.close()
b.close()
one = len(toprocess[0])
#two = lengths[2]
#three = lengths[3]
print one
#(av,st) = avsterr(lengths)
#print "%f +/- %f" % (av,st)

输出:

barcode: ATTAG
S01 ATTAGAAAAAAA

seq: ATTAGAAAAAAA
something
Checking sequences
Traceback (most recent call last):
  File "./FinalProject.py", line 40, in <module>
    toprocess[barcodeCounter].extend(outseq.strip)
IndexError: list index out of range

这是我基于它的代码。

sequenceCounter = -1
for line in handle:
        if line[0] == ">":
                sequenceCounter = sequenceCounter + 1
#               print "seqid %s\n" % line
                seqidList.append(line)
                seqList.append("")
        if line[0] != ">":
                seqList[sequenceCounter] = seqList[sequenceCounter] + line.strip()

编辑: 添加了枚举函数并注释掉了barcodeCounter的东西。

barcode = sys.argv[1]
sequence = sys.argv[2]
lengths = []
toprocess = []
b = open(barcode,"r")
#barcodeCounter = -1
for barcodeCounter, barcode in enumerate(b):
#       barcodeCounter = barcodeCounter + 1
        barcode = barcode.strip()
        print "barcode: %s" %  barcode
        handle = open(sequence, "r")
        for line in handle:
                print line
                seq = line.split(' ',1)[-1].strip()
                print "seq: %s" % seq
                potential_barcode = seq[0:len(barcode)]
                print "something"
                if potential_barcode == barcode:
                        print "Checking sequences"
                        outseq = seq.replace(potential_barcode, "", 1)
                        outseq_length = [len(outseq)]
                        toprocess.append("")
#                       toprocess[barcodeCounter] += outseq.strip
                        toprocess[barcodeCounter].append(outseq.strip) #AttributeError line 40
#                       toprocess[barcodeCounter] = toprocess[barcodeCounter] + outseq.strip
                        print "outseq: %s" % outseq
                        print "Barcodes to be processed: %s" % toprocess[barcodeCounter]
                        print "BC: %i" % barcodeCounter
        handle.close()
b.close()

新错误:

barcode: ATTAG
S01 ATTAGAAAAAAA

seq: ATTAGAAAAAAA
something
Checking sequences
Traceback (most recent call last):
  File "./FinalProject.py", line 40, in <module>
    toprocess[barcodeCounter].append(outseq.strip)
AttributeError: 'str' object has no attribute 'append'

没有问题的代码:

barcode = sys.argv[1]
sequence = sys.argv[2]
lengths = []
toprocess = []
b = open(barcode,"r")
#barcodeCounter = -1
for barcodeCounter, barcode in enumerate(b):
#       barcodeCounter = barcodeCounter + 1
        barcode = barcode.strip()
        print "barcode: \n%s\n" %  barcode
        handle = open(sequence, "r")
        for line in handle:
                print line
                seq = line.split(' ',1)[-1].strip()
                print "seq: %s" % seq
                potential_barcode = seq[0:len(barcode)]
#               print "something"
                if potential_barcode == barcode:
                        print "Checking sequences"
                        outseq = seq.replace(potential_barcode, "", 1)
                        outseq_length = [len(outseq)]
                        toprocess.append("")
                        toprocess[barcodeCounter] = toprocess[barcodeCounter] + outseq

@abarnert你很有帮助,谢谢。我有时(大多数时候)编程时,我不是最聪明的人。我还必须更改添加新序列的方式,因为它们是str而不是list

2 个答案:

答案 0 :(得分:0)

你实际上有两个问题。

首先,您从1而不是0开始计算。您在barcodeCounter处开始0,然后在使用之前递增它。这意味着,如果你有3个条形码,那么你试图设置toprocess[1],然后设置toprocess[2],然后设置toprocess[3],最后一个设置为{{1 }}

请注意,您使用的代码以IndexError而不是sequenceCounter = -1开头,以避免此问题。

然而,这个问题有一个更简单的解决方案:使用0为你做计数:

enumerate

无需记住是从-1,0或1开始,还是在哪里进行递增,或者其中任何一个;它只会自动获取数字0,1,2等,直到for barcodeCounter, barcode in enumerate(b):

其次,即使您计算正确,len(b)-1的大小也不同toprocess。事实上,它完全是空的,因此b 总是会引发异常。

要将新值附加到toprocess[anything]的末尾,请调用list方法:

append

再次注意,在执行toprocess.append(…) 之前,您所基于的代码始终会执行seqList.append("")。 (请注意,它有点棘手 - 有时它seqList[sequenceCounter] =并递增append,有时它不会,并且使用之前的sequenceCounter值分配给seqList[sequenceCounter]。)必须做同等的事情。

答案 1 :(得分:0)

代码

listVariable[indexNumber]

专门用于访问列表变量中已存在的内容。你给它的数字告诉Python你要查找的列表的哪一部分。值得注意的是,列表从0开始计数而不是1.所以下面的代码:

list = ["a","b","c","d"]
print list[0]
print list[3]
print list[1]
print list[-1]

将导致打印

a #index 0
d #index 3
b #index 1
d #index -1

(减去索引实际上从结尾算起,所以-1给你d,而-2会导致c)

当您提供列表没有存储任何内容的数字时,会发生indexError。如果我试图调用list [4],我会得到一个索引错误,因为它不存在,就像我试图调用一个不存在的变量一样。

与字典不同,您无法通过提供非现有索引来设置列表值。你需要使用像append这样的方法,或者扩展而不是你在给出索引然后调用extend函数的方式。严格来说

list[3].append("e")

告诉Python采用存储在列表[3]中的值并附加一个&#39; e&#39;那个,而不是整个列表本身。

list.append("e")

这实际上会将e添加到我的列表中。