有没有一种方法可以递归执行此功能?

时间:2019-04-20 15:33:29

标签: python list for-loop recursion

我对Python还是比较陌生。有没有办法我可以递归执行此功能?我正在寻找匹配对,并排除带有“ +”的不匹配对。

std::wcout.imbue(std::locale("ja_jp.utf-8"));
    wchar_t c_name = L"c:\\my_folder\\フォルダ\\text_file.txt";
    wofstream outfile;
        outfile.open(c_name,ios::out );

    if(!outfile)
     {
        cout<<"Path not found\n";    
     }
     outfile << "Hello world\n";
     outfile.close();

2 个答案:

答案 0 :(得分:0)

这不适合评论,因此这里是备注(不是答案)。该代码是非常多余的,这使事情很难识别。等效于:

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 "+"

答案 1 :(得分:0)

它甚至可以做得更简单(与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)。现在,您必须告诉我您要递归哪一部分? 我通常建议不要进行递归,因为会占用大量资源。