是否有递归执行此函数的方法?

2024-10-04 11:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还比较陌生。有没有一种方法可以递归地执行这个函数?我正在寻找匹配对,并排除带有“+”的不匹配对

integ = 3    #number of sequences
evenList = ['GAAGCTCG', 'AAATTT', 'CTCTAGGAC']
oddList = ['CCTCGGGA', 'GGGCCC', 'GAGTACCTG']

def matchList(evenList, oddList, integ):

        indexElement = 0
        indexList = 0
        totalIndexSeq = []
        at_List = ['AT', 'TA', 'at', 'ta']
        gc_List = ['GC', 'CG', 'gc', 'cg']
        for x in evenList:
            indexedSeq = ''
            for y in x:
                if y + oddList[indexList][indexElement] in gc_List:
                    indexedSeq += str(indexElement)
                    indexElement += 1
                elif y + oddList[indexList][indexElement] in gc_List:
                    indexedSeq += str(indexElement)
                    indexElement += 1
                elif y + oddList[indexList][indexElement] in at_List:
                    indexedSeq += str(indexElement)
                    indexElement += 1
                elif y + oddList[indexList][indexElement] in at_List:
                    indexedSeq += str(indexElement)
                    indexElement += 1
                else:
                    indexedSeq += "+"
                    indexElement += 1
            indexList += 1
            indexElement -= indexElement
            totalIndexSeq.append(indexedSeq)
        return (totalIndexSeq)
        #This returns the positions with mismatched pairs omitted by a "+"
# When you print 'totalIndexSeq'
#['0+234+6+']
#['0+234+6+', '++++++']
#['0+234+6+', '++++++', '012++5678']

Tags: inforgcatlistelifstr陌生
2条回答

它可以做得更简单(是关于DNA上的基因编码吗?):

evenList = ['GAAGCTCG', 'AAATTT', 'CTCTAGGAC']
oddList = ['CCTCGGGA', 'GGGCCC', 'GAGTACCTG']

def matchList(evenList, oddList):

        totalIndexSeq = []
        match_list = [('A','T'), ('T','A') ,('G','C'), ('C','G')]
        pairedList=zip(evenList.upper(),oddList.upper()) # tuples from evenList and oddList elements
        for p in pairedList:
            pairs=zip(list(p[0]),list(p[1])) # tuples of even and odd list characters
            indexSeq=[ str(i) if p in match_list else '+' for i,p in enumerate(pairs)]
            totalIndexSeq.append(''.join(indexSeq)) #convert to string and add to list
        return totalIndexSeq

您对列表的长度没有任何限制(实际上您在代码中也没有使用integ)。现在你得告诉我你想递归哪一部分? 我通常建议不要递归,因为资源使用率很高

这不适合评论,所以这里是评论(不是答案)。代码是相当冗余的,这使得事情很难识别。相当于:

integ = 3    #number of sequences
evenList = ['GAAGCTCG', 'AAATTT', 'CTCTAGGAC']
oddList = ['CCTCGGGA', 'GGGCCC', 'GAGTACCTG']

def matchList(evenList, oddList, integ):

        indexElement = 0
        indexList = 0
        totalIndexSeq = []
        at_List = ['AT', 'TA', 'at', 'ta']
        gc_List = ['GC', 'CG', 'gc', 'cg']

        for x in evenList:
            indexedSeq = ''

            for y in x:
                if y + oddList[indexList][indexElement] in gc_List + at_List:
                    indexedSeq += str(indexElement)                    
                else:
                    indexedSeq += "+"
                indexElement += 1

            indexList += 1
            indexElement -= indexElement
            totalIndexSeq.append(indexedSeq)

        return totalIndexSeq
        #This returns the positions with mismatched pairs omitted by a "+"

相关问题 更多 >