如何使用嵌套循环,遍历数组元素?

时间:2019-03-28 16:49:05

标签: python fasta

来自R背景,我想尝试在python中嵌套for循环。我在下面的代码中遍历types的每个迭代时遇到麻烦。我的代码适用于types[0],但不适用于后续迭代。我该如何解决?

import sys
import os
myfasta = sys.argv[1]
fasta = open(myfasta)
types = ['CDS', 'tRNA', 'rRNA']

for type in range(len(types)):
    print('My type index is: ' + str(type))
    flag = False
    for line in fasta:
        if line.startswith('>') and types[type] in line:
            flag = True
        elif line.startswith('>'):
          flag = False
        if flag:
            print(line.strip())

myfasta

>1_CDS
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
GGGGTTTTTTT
>2_CDS
TTAAAAATTTCTGGGCCCCGGGAAAAAA
>3_CDS
TTTGGGAATTAAACCCT
>4_CDS
TTTGGGAATTAAACCCT
>5_rRNA
TTAAAAATTTCTGGGCCCCGGGAAAAAA
>6_tRNA
TTAAAAATTTCTGGGCCCCGGGAAAAAA

预期结果:

My type index is: 0
>1_CDS
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
GGGGTTTTTTT
>2_CDS
TTAAAAATTTCTGGGCCCCGGGAAAAAA
>3_CDS
TTTGGGAATTAAACCCT
>4_CDS
TTTGGGAATTAAACCCT
My type index is: 1
>5_rRNA
TTAAAAATTTCTGGGCCCCGGGAAAAAA
My type index is: 2
>6_tRNA
TTAAAAATTTCTGGGCCCCGGGAAAAAA

2 个答案:

答案 0 :(得分:2)

您不能一次加载文件并根据需要对其进行遍历。

open()返回一个迭代器,该迭代器只能使用一次。

调整您的代码以遍历文件多次

import sys
import os
myfasta = sys.argv[1]
types = ['CDS', 'tRNA', 'rRNA']

for type in range(len(types)):
    print('My type index is: ' + str(type))
    flag = False
    fasta = open(myfasta)
    for line in fasta:
        if line.startswith('>') and types[type] in line:
            flag = True
        elif line.startswith('>'):
          flag = False
        if flag:
            print(line.strip())

答案 1 :(得分:1)

尝试以下代码:

import sys
import os

myfasta = sys.argv[1]
with open(myfasta) as f:
    lines = f.readlines()
types = ['CDS', 'tRNA', 'rRNA']

for type_index, type in enumerate(types):
    print('My type index is:', type_index)
    flag = False
    for line in lines:
        if line.startswith('>') and type in line:
            flag = True
        elif line.startswith('>'):
            flag = False
        if flag:
            print(line.strip())

输出

My type index is: 0
>1_CDS
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
AAAAATTTCTGGGCCCCAAAAATTTCTGGGCCCC
GGGGTTTTTTT
>2_CDS
TTAAAAATTTCTGGGCCCCGGGAAAAAA
>3_CDS
TTTGGGAATTAAACCCT
>4_CDS
TTTGGGAATTAAACCCT
My type index is: 1
>6_tRNA
TTAAAAATTTCTGGGCCCCGGGAAAAAA
My type index is: 2
>5_rRNA
TTAAAAATTTCTGGGCCCCGGGAAAAAA