[英]For loop prints only last value in Python
I am a newbie at programming, and I have been learning Python for a very short time.我是编程新手,我学习 Python 的时间很短。 Below, I tried to write a code, which counts nucleotides in a sample DNA sequence (A question from ROSALIND).下面,我尝试编写一个代码,计算样本 DNA 序列中的核苷酸(来自 ROSALIND 的问题)。
nucleotides=['A','C','G','T']
string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
for n in nucleotides:
a = string.count (n)
print ("The count for",n,"is:",a)
The output is:输出是:
The count for T is: 21
The problem is that my code prints only the result of last element in "nucleotides" array, which is 'T'.问题是我的代码只打印“核苷酸”数组中最后一个元素的结果,即“T”。 I know that I am asking a silly question but I tried to find an answer by searching on both here and web, and I wasn't successful.我知道我在问一个愚蠢的问题,但我试图通过在此处和网络上搜索来找到答案,但没有成功。 This is why, I would be very appreciated if you could correct the code and made an explanation to me why my loop did not print counts for each nucleotide.这就是为什么,如果您能更正代码并向我解释为什么我的循环没有打印每个核苷酸的计数,我将不胜感激。
Many thanks!非常感谢!
I would check the indentation in your code as it is incorrect in your question.我会检查您代码中的缩进,因为它在您的问题中不正确。 This snippet should work.这个片段应该工作。
nucleotides=['A','C','G','T']
string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
for n in nucleotides:
a = string.count (n)
print ("The count for",n,"is:",a)
Your issue was the indentation as pointed out by other answers.正如其他答案所指出的,您的问题是缩进。
Alternatively , you can use Counter from collections
to get a dictionary containing the frequency of occurrence of each letter.或者,您可以使用Counter from collections
来获取包含每个字母出现频率的字典。 Then just loop over your nucleotides
to print the frequency.然后循环你的nucleotides
来打印频率。
from collections import Counter
nucleotides=['A','C','G','T']
string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
counts = Counter(string)
for n in nucleotides:
a = counts[n]
print ("The count for",n,"is:",a)
Output输出
The count for A is: 20
The count for C is: 12
The count for G is: 17
The count for T is: 21
Your code is actually working, except the fact you add an extra tab in the for loop (wrong indentation).您的代码实际上可以正常工作,除了您在 for 循环中添加了一个额外的制表符(错误的缩进)。 You could try this slightly improve variation instead:你可以试试这个稍微改进的变化:
# define nucleotides
nucleotides=['A','C','G','T']
# define dna chain
string='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
# iterate through the dna chain and count
# the number of appearances for each nucelotide.
for nucl in nucleotides:
x = string.count(nucl)
print ("The count for " + nucl + " is: " + str(x))
I tried the code on sublime and got the following result.我在 sublime 上尝试了代码并得到了以下结果。
('The count for', 'A', 'is:', 20)
('The count for', 'C', 'is:', 12)
('The count for', 'G', 'is:', 17)
('The count for', 'T', 'is:', 21)
I think the problem with your code is that you indented "for loop" unnecessarily.我认为您的代码的问题在于您不必要地缩进了“for 循环”。 make sure to use the right indentation.确保使用正确的缩进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.