【问题标题】:Python: Constructing & Printing matricesPython:构造和打印矩阵
【发布时间】:2015-04-24 10:01:13
【问题描述】:

我需要创建一个计算 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))

任何帮助将不胜感激。

【问题讨论】:

    标签: python python-2.x


    【解决方案1】:

    试试这个:

    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
    

    【讨论】:

    • 哇哦,非常感谢!我现在了解您如何为打印函数传递参数。我还看到了如何需要 compute_LCS 函数的第一条 LCSmatrix 行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多