来自R background,我想在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
您无法加载文件一次,并根据需要迭代它。
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())
试试这段代码:
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