如何使内循环考虑外循环的每次迭代? - python

'A', 'U', 'G' or 'C'组成的三字母集定义为密码子。每个密码子对应20个字母之一。这些字母(氨基酸)的集合定义为蛋白质。文件“ codons.txt”包含密码子和相应的字母。

接下来的问题是:内部for循环仅工作一次-它仅将txt文件中的行与第一个密码子进行比较。然后,据我所知,该方法跳过了内部循环。

码:

path = r'C:\Users\...\codons.txt'
f = open(path, 'r')

def prot(DNA):
    protein = ''
    a = True
    for i in range (0, len(DNA)-2,3):
        codon = DNA[i:i+3:1]
        print(codon)
        for line in f:
            if line[0:3:1] == codon:
                protein += line[4:5:1]
                print(protein)
    return protein


prot('AGUCAGGAUAGUCUUA')

输出:

 AGU
 S
 CAG
 GAU
 AGU
 CUU

接下来的问题是:如何使每个密码子的内环起作用?

参考方案

遍历文件(for line in f:)时,到达文件末尾时它将停止。

您可以:

使用f.seek(0)将文件阅读器位置重置为文件的开头
或更改循环的顺序,以便您仅在文件上迭代一次。

def prot(DNA):
    protein = ''

    with open(path, 'r') as f:
        for line in f:
            for i in range (0, len(DNA)-2,3):
                codon = DNA[i:i+3:1]
                print(codon)

                    if line[0:3:1] == codon:
                        protein += line[4:5:1]
                        print(protein)

    return protein

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…