码迷,mamicode.com
首页 > 其他好文 > 详细

01 A Counting DNA Nucleotides

时间:2017-07-29 15:26:57      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:aaaaa   logs   sel   val   osal   XML   data-   number   highlight   

Problem

string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains.

An example of a length 21 DNA string (whose alphabet contains the symbols ‘A‘, ‘C‘, ‘G‘, and ‘T‘) is "ATGCTTCAGAAAGGTCTTACG."

Given: A DNA string ss of length at most 1000 nt.

Return: Four integers (separated by spaces) counting the respective number of times that the symbols ‘A‘, ‘C‘, ‘G‘, and ‘T‘ occur in ss.

Sample Dataset

AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC

Sample Output

20 12 17 21

方法一:
f = ‘AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC‘
for i in f:
    b = list(f)    # 把‘AAA’变成 [‘A‘,‘A‘‘,A‘]
    c = {}          
    for i in b:
        c[i] = b.count(i)   # 把key 和value 写入字典,如 A:1
print (c.values())  # 最后的结果为 [20,12,21,17]

  方法二:

f = ‘AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC‘
counts = []
for i in [‘A‘,‘C‘,‘G‘,‘T‘]:          # 把输出的顺序定好
    counts.append(f.count(i))
print (‘\t‘.join(map(str, counts)))  #map() 这里的意思是吧输出的[20,12,17,21]变为 20 12 17 21

  






01 A Counting DNA Nucleotides

标签:aaaaa   logs   sel   val   osal   XML   data-   number   highlight   

原文地址:http://www.cnblogs.com/think-and-do/p/7255650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!