代码如下:
from collections import OrderedDict ID_name = ‘‘ temp_dict = OrderedDict() with open(r‘D:\testpy\Rosalind\rosalind_gc.txt‘,‘r‘) as f: for line in f: if line.startswith(‘>‘): ID_name = line.strip() temp_dict[ID_name] = ‘‘ #之前一直不会将ID和seq存到序列中,这里给temp_dicr[ID_name]一个空值就是为了将ID存进字典里。 else: temp_dict[ID_name] += line for ID,seq in temp_dict.items(): GC_num = seq.count(‘G‘) + seq.count(‘C‘) GC_ration = GC_num/len(seq) print (ID + " ====> " +"%.8f"%GC_ration)
时间: 2024-10-29 19:10:43