Python:构建&印刷矩阵

时间:2015-02-23 06:00:22

标签: python python-2.x

我需要创建一个计算LCS的矩阵,然后将其打印出来。这是我的代码,但是我的打印功能有问题(不知道如何将LCSmatrix值用于打印)

def compute_LCS(seqA, seqB):

    for row in seqA:
    for col in seqB:
        if seqA[row] == seqB[col]:
            if row==0 or col==0:
                LCSmatrix(row,col) = 1
            else:
                LCSmatrix(row,col) = LCS(row-1,col-1) + 1 
        else: 
            LCSmatrix(row,col) = 0
return LCSmatrix


def printMatrix(parameters...):
    print ' ',
    for i in seqA:
          print i,
    print
    for i, element in enumerate(LCSMatrix):
          print i, ' '.join(element)

matrix = LCSmatrix


print printMatrix(compute_LCS(seqA,seqB))

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

试试这个:

seqA='AACTGGCAG'
seqB='TACGCTGGA'

def compute_LCS(seqA, seqB):
    LCSmatrix = [len(seqB)*[0] for row in seqA]
    for row in range(len(seqB)):
        for col in range(len(seqA)):
            if seqB[row] == seqA[col]:
                if row==0 or col==0:
                    LCSmatrix[row][col] = 1
                else:
                    LCSmatrix[row][col] = LCSmatrix[row-1][col-1] + 1_
            else:
                LCSmatrix[row][col] = 0
    return LCSmatrix

def printMatrix(seqA, seqB, LCSmatrix):
    print ' '.join('%2s' % x for x in ' '+seqA)
    for i, element in enumerate(LCSmatrix):
        print '%2s' % seqB[i], ' '.join('%2i' % x for x in element)

matrix = compute_LCS(seqA, seqB)
printMatrix(seqA, seqB, matrix)

以上产生:

    A  A  C  T  G  G  C  A  G
 T  0  0  0  1  0  0  0  0  0
 A  1  1  0  0  0  0  0  1  0
 C  0  0  2  0  0  0  1  0  0
 G  0  0  0  0  1  1  0  0  1
 C  0  0  1  0  0  0  2  0  0
 T  0  0  0  2  0  0  0  0  0
 G  0  0  0  0  3  1  0  0  1
 G  0  0  0  0  1  4  0  0  1
 A  1  1  0  0  0  0  0  1  0