Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python While循环多打印GC百分比_Python_Loops_While Loop_Break - Fatal编程技术网

Python While循环多打印GC百分比

Python While循环多打印GC百分比,python,loops,while-loop,break,Python,Loops,While Loop,Break,我正试图写一个程序来获取DNA序列中GC的百分比 print("Enter With Z to finish") while True: sequence_dna = input("Enter with sequence:") print("DNA Sequence:\t\t",sequence_dna) dna = sequence_dna DNA = dna.upper() DNAlist = list(DNA) CountC = sequen

我正试图写一个程序来获取DNA序列中GC的百分比

print("Enter With Z to finish")
while True:
    sequence_dna = input("Enter with sequence:")
    print("DNA Sequence:\t\t",sequence_dna)
    dna = sequence_dna
    DNA = dna.upper()
    DNAlist = list(DNA)
    CountC = sequence_dna.count("C")
    CountG = sequence_dna.count("G")
    GC = (100*(CountC+CountG)/float(len(dna)))
    print("the percentage of GC is: %.2f"%GC)
我还需要编写一个程序,要求输入多个DNA字符串,而不是像我那样只输入一个。我该怎么办? 程序需要使用
break
命令结束,并指示哪个DNA序列具有最多GC

例如:

DNA序列中的
(0)
DNA序列的超出百分比(0)
在这里输入代码
在DNA序列中(1)
DNA序列的超出百分比(1)
输入代码
在DNA序列中(2)
DNA序列的超出百分比(2)
打破
DNA序列(1)具有最高的GC's百分比
这应该可以:

from operator import itemgetter    

print("Enter With 'z' to finish") #must be lowercase 'z'

dna_list = []

while True:
    sequence_dna = input("Enter DNA sequence: ")
    print("DNA Sequence:\t\t",sequence_dna)
    dna = sequence_dna
    DNA = dna.upper()

    dna_list.append(DNA)

    if sequence_dna == 'z':
        break

gc_percentages = []

for sequence in dna_list[:-1]: # Takes all elements of the list except for the empty entry in the last position.

    CountC = sequence.count("C")
    CountG = sequence.count("G")
    GC = str((((CountC + CountG)/len(sequence)) * 100)) + '%'
    print("The percentage of GC content for the sequence in list position %s is: %s"%(dna_list.index(sequence), GC))

    gc_percentages.append((sequence, (((CountC + CountG)/len(sequence)) * 100) ))


sorted_gc_scores = sorted(gc_percentages, key= itemgetter(1), reverse= True)

print ("\nThe sequence with the highest GC percentage of %s is:\n%s" %(str(sorted_gc_scores[0][1]) + '%', sorted_gc_scores[0][0]))

代码运行后,您可以参考dna列表中哪个序列的GC百分比最高。

@SiHa,我给生物信息学贴上了标签,因为这是从事生物信息学工作的人会问的一个非常常见的问题。@K.Land\u bioinfo:可能,但这纯粹是一个编程问题。代码在生物信息学环境中使用的事实与此无关;问题只是如何存储多个结果并返回最大值。