Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器错误:列表索引超出范围,不确定原因_Python_Attributeerror_Indexoutofrangeexception - Fatal编程技术网

Python 索引器错误:列表索引超出范围,不确定原因

Python 索引器错误:列表索引超出范围,不确定原因,python,attributeerror,indexoutofrangeexception,Python,Attributeerror,Indexoutofrangeexception,我想让程序做的是获取与特定条形码相关的序列,并执行定义的功能(序列的平均长度和标准偏差,减去条形码和非相关的txt,由相同的条形码标识)。我写了一些类似的东西,并基于类似的程序,但我不断得到一个索引器。我们的想法是,所有带有第一个条形码的序列将被处理为barcodeCounter=0,第二个为barcodeCounter=1,以此类推。希望这是足够的信息,抱歉,如果它很混乱 输入: import sys import math def avsterr(x): ave = sum

我想让程序做的是获取与特定条形码相关的序列,并执行定义的功能(序列的平均长度和标准偏差,减去条形码和非相关的txt,由相同的条形码标识)。我写了一些类似的东西,并基于类似的程序,但我不断得到一个索引器。我们的想法是,所有带有第一个条形码的序列将被处理为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
某物
检查序列
回溯(最近一次呼叫最后一次):
文件“/FinalProject.py”,第40行,在
toprocess[barcodeCounter].extend(outseq.strip)
索引器:列表索引超出范围
这就是我所依据的代码

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
某物
检查序列
回溯(最近一次呼叫最后一次):
文件“/FinalProject.py”,第40行,在
toprocess[barcodeCounter]。追加(outseq.strip)
AttributeError:“str”对象没有属性“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

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

这里实际上有两个问题


首先,从1开始计算,而不是从0开始。在
0
处启动
barcodeCounter
,然后在使用它之前递增它。这意味着,如果您有3个条形码,您将尝试设置
toprocess[1]
,然后
toprocess[2]
,然后
toprocess[3]
,最后一个将是
索引器

请注意,基于它的代码以
sequenceCounter=-1
开始,而不是以
0
开始,以避免此问题

但是,有一个更简单的解决方案:使用
enumerate
为您进行计数:

for barcodeCounter, barcode in enumerate(b):
不需要记住是从-1、0还是1开始,或者从何处开始递增,或者任何这些;它只会自动获取0、1、2等数字,直到
len(b)-1


第二,即使正确计数,
toprocess
的大小也与
b
的大小不同。事实上,它是完全空的,所以
toprocess[任何东西]
总是会引发异常

要将新值追加到
列表的末尾
,请调用
追加
方法:

toprocess.append(…)
再次注意,在执行
seqList[sequenceCounter]=
之前,您所基于的代码总是执行
seqList.append(“”
)。(请注意,有时它会添加
并递增
sequenceCounter
,有时两者都不做,并使用前面的
sequenceCounter
值分配给
seqList[sequenceCounter]
)您必须执行等效操作。

代码

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)

索引器是指当您给出一个列表中没有存储的数字时发生的情况。如果我试图调用列表[4],我会得到一个索引错误,因为它不存在,就像我试图调用一个不存在的变量一样

与字典不同,您不能通过提供不存在的索引来设置列表值。您需要使用像append或extend这样的方法,但不需要使用给定索引然后调用extend函数的方法。严格地说

list[3].append("e")
正在告诉Python获取列表[3]中存储的值并在其上附加一个“e”,而不是整个列表本身

list.append("e")

这就是将e添加到我的列表中的原因。

对不起,我以为我输入的所有内容都正确,但修复了它的错误,即直截了当,但您得到了一个索引器,因为您不知道列表在python中是如何工作的。您将
toprocess
初始化为空列表,然后尝试使用
toprocess[barcodeCounter]
访问其不存在的元素,然后再将任何内容放入列表中。你到底希望发生什么?布伦特很好,我不太明白列表是如何工作的。我希望在列表中添加一些内容。条形码文件的内容是什么?您可能需要阅读教程或类似的内容。如果这还不足以完全理解代码所基于的代码,请阅读更多内容或提出问题。在您真正理解其他人的代码之前,不要尝试编写与其他人的代码具有相同功能的代码。@l4mpi:他在这两个方面都有问题。他试图复制的代码附加值,然后覆盖它们,从0开始计数。他不是先追加,而是从1开始计算,他必须解决这两个问题,否则他将得到
索引器
s;仅仅修复一个是不行的。因此,使用枚举函数将不需要barcodeCounter=barcodeCounter+1并将barcodeCounter设置为-1?@slick1092:是的,完全正确。但是,同样,您需要解决第二个问题以及第一个问题,一旦您解决了第二个问题,您甚至可能不再需要
barcodeCounter
…我按照您的建议添加了
enumerate
函数。我以前使用过
.append
,但是我得到了一个属性错误,这就是我尝试
.extend
的原因。我相信我仍然需要
条形码计数器
,因为我需要能够识别序列