||
Counting DNA nucleotides
An example of a length 21 DNA string (whosealphabet contains the symbols 'A', 'C', 'G', and 'T') is"ATGCTTCAGAAAGGTCTTACG."
Given: ADNA string s of length at most 1000 nt.
Return: Fourintegers (separated by spaces) counting the respective number of times that thesymbols 'A', 'C', 'G', and 'T' occur in s.
Sample Dataset
AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC
Sample Output
20 12 17 21
以上的例子可以有两种以下解决方案
第一种为较笨拙的if else 语句,代码如下:
#!/usr/bin/python
s='atcggtcaggttggttaaccgt'
a=0
b=0
c=0
d=0
for i in s:
if i=='a':
a+=1
elif i=='c':
b+=1
elif i=='g':
c+=1
else:
d+=1
print a,b,c,d
第二种方法可以调用python的内建函数count()
#!/usr/bin/python
s="atcgatcgatttgggcccttaagg"
print s.count('a'),
print s.count('t'),
print s.count('c'),
print s.count('g')
注:使用python时注意缩进
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 23:32
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社