Python的列表等变量的声明必须要在函数定义与函数调用之前

  • Post author:
  • Post category: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/']



版权声明:本文为a787264137原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。