簡體   English   中英

IndexError:列表索引超出范圍錯誤,如何修復代碼?

[英]IndexError: list index out of range error, How do I fix the code?

import os 

import sys

import numpy as np

from proteinss import ProteinSS

map_dssp_3_alphabet = {"H":1,"I":1,"G":1,"E":2,"B":2,"T":3,"S":3, "_":3, "?":3}
map_dssp_8_alphabet = {"H":1,"I":2,"G":3,"E":4,"B":5,"T":6,"S":7, "_":8, "?":8}

def extract_file(ss_file):
    print ("Extracting :"), ss_file

fi = open(ss_file, 'rU')
seq_string = ""
ss_string  = ""
alignments = []
for line in fi:
    if line.startswith("RES:"):
       seq_string = line.split(":")[1].rstrip('\n')

    if line.startswith("DSSP:"):
       ss_string = line.split(":")[1].rstrip('\n')

    if line.startswith("align"):
       alignment = line.split(":")[1].rstrip('\n')
       alignments.append( alignment.split(",")[0:-1] )

seq_l = seq_string.split(",")[0:-1]
ss_l  = ss_string.split(",")[0:-1]

prot = ProteinSS(seq_l, ss_l)

for al_ in alignments:
    prot.add_alignment(al_)

return prot


def prepare_db(cb513_path, db_ofile, db_classes, wsize, alphabet):
    fo1 = open(db_ofile, 'w')
    fo2 = open(db_classes, 'w')


for root, dirs, files in os.walk(cb513_path):
    for name in files:
        if name.endswith(( ".all" )):
            ss_file = root + "/" + name
            prot = extract_file ( ss_file )
            if prot.is_valid():
                s1, s2 = prot.get_db_strings(wsize, map_dssp_3_alphabet, True)
                fo1.write(s1)
                fo2.write(s2)

fo1.close()
fo2.close()


if __name__ == "__main__":
    cb513_path = '../data/CB513'
    classes_ofile   = "../data/db/ss_a3.dat" 

window_width = int( sys.argv[5] ) 
db_ofile = "../data/db/aa_w" + str(window_width) + "_a3.dat"

if sys.argv[2] == "3":
    alphabet = map_dssp_3_alphabet
elif sys.argv[2] == "8":
    alphabet = map_dssp_8_alphabet
else:
    print ("Alphabet not recognized: choose between 3 or 8")
    sys.exit(1)

pubs_data = prepare_db(cb513_path, db_ofile, classes_ofile, window_width, alphabet)

我在遇到錯誤:

window_width = int(sys.argv [5])

IndexError:列表索引超出范圍

當我嘗試使用spyder運行代碼時,編碼顯示錯誤列表超出范圍錯誤,我應該更改指定的尺寸還是使用任何其他功能?

這是我用於蛋白質二級結構預測的代碼之一,尤其是用於創建數據庫的代碼。

IndexError是Python中最基本和常見的異常之一,因為每次嘗試訪問列表邊界之外的索引時都會引發該異常,這是因為我正在嘗試訪問列表邊界之外的東西。

如何解決以上問題?

sys.argv在Python中定義為列表,其中包含要執行任何程序時傳遞給腳本的命令行參數。

您可以在執行列表時檢查傳遞的參數,只需在程序開始時打印sys.argv的長度即可。

在程序開始時包括以下行:

print(len(sys.argv))

從命令行開始,如果我正在命令行以下程序中執行

python demo.py 

那么sys.argv的長度將為1,而sys.argv [0]的值將為demo.py。

檢查后,可以根據需要修改sys.argv索引值。 希望這能解決您的問題。

PS在您的程序中此行寫錯了:

print ("Extracting :"), ss_file

更正為

 print ("Extracting :"+ ss_file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM