python - 如何在找到值后立即在字符串搜索中跳到索引? Python

标签 python search

我的程序现在有两个函数。

get_orf(dna) 将名为 dna 的字符串作为输入。如果字符串开始 使用起始密码子“ATG”,然后 get_orf 以 3 的倍数搜索任何终止密码子。

如果找到其中之一,则返回 ORF(“ATG”和终止密码子之前的序列)。

orf_List = []
stopCodons = ["TAG", "TAA", "TGA"]

def get_orf(dna):
    #checks to see if the first three amino acids are ATG
    if dna[:3] == "ATG":

        #if the first three are amino acids, 
        #it checks the dna string in multiples of 3 uisng the range fucntion
        for k in range(0, len(dna), 3):

            #checking for stop codons in the dna string
            if any(s in dna[k:k+3] for s in stopCodons):

                #if a stop codon is found, it returns it
                return dna[0:k]
        #prints No Orf if there are no stop codons found
        else:
            print("No ORF.")

    #prints No Orf if the dna does not start with ATG
    else:
        print("No ATG IN BEGINNING.")

one_frame(dna) 以 DNA 字符串作为输入。

one_frame 以三的倍数从左到右搜索该字符串。什么时候 它命中起始密码子“ATG” 它在从该起始密码子开始(直到结束)的字符串切片上调用 get_orf 以取回 ORF。该 ORF 被添加到 ORF 列表中,并且然后函数在 DNA 串中向前跳到我们刚找到的 ORF 之后的点并开始寻找 下一个ORF。重复此过程,直到我们遍历整个 DNA 串。

def one_frame(dna):

    i = 0
    while i < len(dna):
        for i in range(0, len(dna), 3):

            if "ATG" in dna[i:i+3]:

                newOrf = get_orf(dna[i:])
                orf_List.append(newOrf)



            #i don't know how to skip ahead

    print(orf_List) 
    return(orf_List)

当我调用 one_frame("ATGCCCATGCCCCCCTAG") 时,我无法弄清楚如何向前跳到找到的 ORF 的最后一个索引,以便我可以继续搜索。有什么帮助吗?

使用此代码,它会打印出“ATGCCCATGCCCCCCTAG”和“ATGCCCCCCTAG”。不应打印较小的,因为它位于较大的内部。

最佳答案

我建议您摆脱 one_frame 中的 for 循环并将其实现为 while 循环(看起来您已经尝试过了)。

然后你可以手动控制i值并将找到的ORF的长度添加到它以向前跳过。

def one_frame(dna):
    orf_list = []
    i = 0
    while i < len(dna):
        if "ATG" == dna[i:i+3]:
            new_orf = get_orf(dna[i:])
            if new_orf:
                orf_list.append(new_orf)
                i += len(new_orf)
                continue
        i += 3

    print(orf_list)
    return(orf_list)

关于python - 如何在找到值后立即在字符串搜索中跳到索引? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52637164/

相关文章:

php - 包含标签的全文搜索

c++ - 二进制搜索给出的结果略有不准确

search - Elasticsearch 分析 API 异常

python在字符串中求解方程

java - 从数据库中搜索后对结果进行评分

javascript - XPages Domino 搜索为一个特定的搜索词产生不正确的结果

python - Box Python SDK 'NoneType' 对象不可调用

python - 如何删除子图之间的 "empty"空间?

python - 处理图像作为mnist模型的输入

java - 带线程的格乘,效率更高吗?