1. 程式人生 > >Python的列表等變數的宣告必須要在函式定義與函式呼叫之前

Python的列表等變數的宣告必須要在函式定義與函式呼叫之前

原始碼

—–這個程式碼作用是呼叫Linux應用終端操作來實現計算,並把計算結果取出來,組成相似矩陣,有五組值也就是有五組相似矩陣,這個是我做蛋白質結構預測用到的,自己寫的,分享出來給大家,如有疑惑,歡迎評論區交流。

# 此程式碼主要為說明問題,後面我會貼上完整程式碼以及資料。
def cal_simi(n):
    file = os.popen('cd '+path[n-1]+';ls').readlines()
    RMSD_simi = []
    TMscore_simi = []
    MasSubscore_simi = []
    GDTTSscore_simi = []
GDTHAscore_simi = [] for fileName1 in file: temp1 = [];temp2=[];temp3=[];temp4=[];temp5=[] for fileName2 in file: comm = 'cd /home/stern/Desktop/TMscore;' + 'sudo ./TMscore ' + path[n-1] + fileName1[:-1] +' '+ path[n-1] + fileName2[:-1] # storge the result of the command in data
data = os.popen(comm).readlines() temp1.append(data[14][32:38]);temp2.append(data[16][14:20]);temp3.append(data[17][14:20]) temp4.append(data[18][14:20]);temp5.append(data[19][14:20]) RMSD_simi.append(temp1) TMscore_simi.append(temp2) MasSubscore_simi.append(temp3)
GDTTSscore_simi.append(temp4) GDTHAscore_simi.append(temp5) p1 = np.median(RMSD_simi);p2 = np.median(TMscore_simi);p3 = np.median(MasSubscore_simi) p4 = np.median(GDTTSscore_simi);p5 = np.median(GDTHAscore_simi) for i in range(len(file)): RMSD_simi[i][i] = p1 TMscore_simi[i][i] = p2 MasSubscore_simi[i][i] = p3 GDTTSscore_simi[i][i] = p4 GDTHAscore_simi[i][i] = p5 return RMSD_simi,TMscore_simi,MasSubscore_simi,GDTTSscore_simi,GDTHAscore_simi Xn = ['1abv_','1af7_','1cqkA','1csp_','1dcjA','256bA'] path = ['/home/stern/Desktop/1abv_/','/home/stern/Desktop/1af7_/','/home/stern/Desktop/1cqkA/', '/home/stern/Desktop/1csp_/','/home/stern/Desktop/1dcjA/','/home/stern/Desktop/256bA/'] # fsadf RMSD_simi1 = [] TMscore_simi1 = [] MasSubscore_simi1 = [] GDTTSscore_simi1 = [] GDTHAscore_simi1 = [] RMSD_simi1,TMscore_simi1,MasSubscore_simi1,GDTTSscore_simi1,GDTHAscore_simi1=cal_simi(1) print RMSD_simi1[1][1] print TMscore_simi1[2][2] print MasSubscore_simi1[3][3] print GDTTSscore_simi1[4][4] print GDTHAscore_simi1[5][5]

執行結果

—–執行結果,錯誤如下,如果把# fsadf後面的列表的宣告提到函式定義的前面,則錯誤就沒有了,列表的宣告必須要在函式的呼叫之前,這個就更不用講了,如果你不在函式呼叫之前的話,會提示列表不存在。。

Xn = ['1abv_','1af7_','1cqkA','1csp_','1dcjA','256bA']
  File "<stdin>", line 30
    Xn = ['1abv_','1af7_','1cqkA','1csp_','1dcjA','256bA']
     ^
SyntaxError: invalid syntax
>>> path = ['/home/stern/Desktop/1abv_/','/home/stern/Desktop/1af7_/','/home/stern/Desktop/1cqkA/',
...     '/home/stern/Desktop/1csp_/','/home/stern/Desktop/1dcjA/','/home/stern/Desktop/256bA/']