Python 跟踪函数返回时出错

Python 跟踪函数返回时出错,python,Python,我试图在这段代码中执行动态规划以进行近似模式匹配。一旦创建了矩阵,我就试图使用函数trace追溯到我的答案。但是当我运行程序时,trace函数没有返回任何结果,程序也没有终止 class Structure : def __init__(self): self.vertical =[] self.horizontal =[] self.diagnol=[] def merge(self, s): for i in

我试图在这段代码中执行动态规划以进行近似模式匹配。一旦创建了矩阵,我就试图使用函数trace追溯到我的答案。但是当我运行程序时,trace函数没有返回任何结果,程序也没有终止

class Structure :
    def __init__(self): 
        self.vertical =[]
        self.horizontal =[]
        self.diagnol=[]
    def merge(self, s):
        for i in s.vertical :
            self.vertical.append(i)
        for i in s.horizontal :
            self.horizontal.append(i)
        for i in s.diagonal:
            self.diagonal.append(i)
    def __str__(self):

        return "horizontal \n"+str(self.horizontal) +"\n vertical \n"+str(self.vertical)+"\n diagonal"+str(self.diagonal)



def posList(pattern, text): #determine the positions in the matrix
    retList = list()
    textList = [x for x in text.strip()]
    for i, char1 in enumerate(pattern):
        textPos = [j for j, char2 in enumerate(textList) if char1==char2]
        for j in textPos:
            retList.append((i+1,j+1))
    return retList

def trace(M,text,pattern,k) :
    positions=posList(pattern,text)

    struct = Structure()
    for i in range(0,2):
        for j in range(0,7):
            while M[i,j]<=k and M[2,j]!=0:
                if M[i,j] == M[i,j-1]+1:
                    struct.horizontal.append(j)
                    j -= 1
                elif M[i,j] == M[i-1,j]+1:
                    struct.vertical.append(i)
                    i -= 1
                elif (i+1,j+1)in positions and M[i,j]==M[i-1,j-1] :
                    struct.diagonal.append((i,j))
                    i -= 1
                    j -= 1
                elif (i+1,j+1) not in positions and M[i,j]==M[i-1,j-1]+1 and M[i,j]==M[i-1,j]+1:
                    struct.vertical.append(i)
                    i-=1
                    print "error"
                elif (i+1,j+1) not in positions and M[i,j]==M[i-1,j-1]+1 and M[i,j]==M[i,j-1]+1:
                    struct.horizontal.append(j)
                    j-=1
                elif M[i,j]==M[i-1,j]+1 and M[i,j]==M[i,j-1]+1:
                    struct.vertical.append(i)
                elif M[i,j]==M[i-1,j]+1 and M[i,j]==M[i,j-1]+1 and M[i,j]==M[i-1,j-1]+1:
                    struct.vertical.append(i)
                    i-=1

                else :
                    pass

    return struct

text='ACAGCAG'
pattern='GC'

n = len(pattern)
m = len(text)

text1=list(text)
pattern1=list(pattern)

M = [[0 0 0 0 0 0 0 0],[1 1 1 1 0 1 1 0],[2 2 1 2 1 0 1 1]]
#perform traceback
s= trace(M,text,pattern,1)
print s
s = trace(M, w, seq, max, 0, n-1)
print str(s)
print seq
result=real_string(s)
print "".join(result)
类结构:
定义初始化(自):
self.vertical=[]
self.horizontal=[]
self.diagnol=[]
def合并(自身):
对于垂直于s的i:
self.vertical.append(i)
对于s.horizontal中的i:
self.horizontal.append(i)
对于s.对角线中的i:
self.diagonal.append(i)
定义(自我):
返回“horizontal\n”+str(self.horizontal)+“\n vertical\n”+str(self.vertical)+“\n diagonal”+str(self.diagonal)
def posList(模式、文本):#确定矩阵中的位置
retList=list()
textList=[x代表text.strip()中的x]
对于i,枚举(模式)中的char1:
textPos=[j代表j,如果char1==char2,则枚举(textList)中的char2]
对于textPos中的j:
retList.append((i+1,j+1))
返回列表
def跟踪(M、文本、模式、k):
位置=位置列表(模式、文本)
struct=Structure()
对于范围(0,2)内的i:
对于范围(0,7)内的j:
而M[i,j]我建议在
while
循环开始后放置。如果您的脚本从未终止,很可能是因为您的条件从未变为
False
,我建议将其放在
while
循环开始之后。如果脚本从未终止,很可能是因为您的条件从未变为
False