【问题标题】:How do I change print to return in a for loop in python?如何更改 print 以在 python 中的 for 循环中返回?
【发布时间】:2017-05-03 16:43:13
【问题描述】:

下面的代码正在做我想做的大部分事情......

我只需要让print 成为return,这样我就可以将数据转储到我正在编写的另一个txt 文件中(f2)。

(另外,print letters 得到的间距不是我想要的,但我想我会在稍后处理它。)

每次我将 print 替换为 return 时,它都会在初始文本文件 (f1) 的第一行之后停止读取。

def DNA2Prot(f1, f2="translated_fasta.txt"):
    fin = open(f1, 'r')
    for letters in fin:
        if letters[0] != ">":
            seqs = letters
            codons = [ ]
            protein = ''
            for i in range(0, len(seqs), 3):
                try:
                    codon = seqs[i:i+3]
                    codons = codon_table[codon]
                    protein = protein+codons
                except KeyError:
                    protein += ""
            print protein
        else:
            print letters
    fin.close()

【问题讨论】:

  • 请修正缩进,在运算符周围添加空格,赋值...另外,粘贴一些示例数据,以便我们了解文件包含的内容
  • 考虑支持有用的答案并接受作为答案,请参阅What does it mean when an answer is "accepted"?
  • 您的代码在遇到无效密码子(被有效跳过)后继续翻译。崩溃或拥有完整的密码子表可能会更好。

标签: python for-loop printing return bioinformatics


【解决方案1】:

改用yield 并将您的函数视为生成器。这样,调用者可以对DNA2Prot 函数生成并从文件中读取的所有蛋白质做他/她喜欢的事情,直到读取整个文件。

def DNA2Prot(f1, f2='translated_fasta.txt'):
    # prefer using `with` to `open` and `close`
    with open(f1, 'r') as fin:
        for letters in fin: 
            if letters[0] != '>':
                seqs = letters
                codons = [ ]
                protein = ''
                for i in range(0, len(seqs), 3):
                    # no need for a try catch, because we can use `get`
                    # get will return None by default if the 
                    # specified `codon` does not appear in 
                    # `codon_table`
                    codon = seqs[i:i + 3]       
                    codons = codon_table.get(codon)
                    if codons:
                        protein += codons
                yield protein
            else:
                yield letters        

现在您必须将DNA2Prot 函数视为Iterator

with open('/path/to/outfile', 'w') as f:
    for protein in DNA2Prot(f1):
        # do something with protein
        print protein

【讨论】:

    【解决方案2】:

    首先要做的事情。当您使用 return 语句时,您是在告诉您的代码从 return 语句所在的位置中断(即离开)。这意味着您的代码将从 fin 开始读取,然后移至第二个 for ,一旦完成(读取该行的所有字母),它将到达您的 return 语句并从 DNA2prot 函数中中断。

    现在,当涉及到文件时,您可以做两件事。首先是使用打印功能将您的输出重定向到文件(不推荐)或正确打开文件并写入它们。

    关于第一个解决方案(假设您使用的是 python 2.7),您可以简单地这样做:

    from __future__ import print_function
    

    当你想使用你的打印语句时,只需写:

    print(protein, file = fin).
    

    但是,如果我是你,我会选择一个更优雅、更干净的解决方案,它不依赖于不必要的导入:

    def DNA2Prot(f1, f2="translated_fasta.txt"):
    with open (f1, 'r+') as fin, open(f2, 'w+') as fin2: #Using the "with-open" statement you don't need to close the file object
        for letters in fin:
            if letters[0]!=">":
                seqs=letters
                codons=[ ]
                protein=''
                for i in range(0,len(seqs),3):
                    try:
                        codon=seqs[i:i+3]
                        codons=codon_table[codon]
                        protein=protein+codons
                    except KeyError:
                         protein+=""
                f2.write(protein) # Write your data to the second file             
    
            else:
                f2.write(letters) 
    

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 2019-07-31
      • 1970-01-01
      • 2014-04-12
      • 2015-12-30
      • 1970-01-01
      • 2014-04-05
      • 2020-02-05
      • 2011-07-05
      相关资源
      最近更新 更多