haiferry的个人博客分享 http://blog.sciencenet.cn/u/haiferry

博文

python 计算Counting DNA nucleotides

已有 2300 次阅读 2015-10-26 22:01 |系统分类:科研笔记| Python, style, color, background, white

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时注意缩进



https://blog.sciencenet.cn/blog-2887147-931227.html


下一篇:DNA 的反向互补
收藏 IP: 159.226.67.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-9-27 23:32

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部