3

In this question, you are given a string s which represents a DNA string. The string s consists of symbols 'A', 'C', 'G', and 'T'. An example of a length 21 DNA string is "ATGCTTCAGAAAGGTCTTACG."

Your task is to write a code which will count the number of times each of the symbols 'A', 'C', 'G', and 'T' occur in s. Your code should generate a list of 4 integers and print it out.

# Here is the DNA string:
    s = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
    # Type your code here

Incorrectly, I wrote the string with spaces between letters.

s='A G C T T T T C A T T C T G A C T G C A A C G G G C A A T A T G T C T C T G T G T G G A T T A A A A A A A G A G T G T C T G A T A G C A G C'
list_of_symbols=s.split(sep=' ')
list_of_symbols
word_count_dictionary={}
for A in list_of_symbols:
    if A not in word_count_dictionary:
        word_count_dictionary[A]=1
    else:
        word_count_dictionary[A]+=1
Austin
  • 25,759
  • 4
  • 25
  • 48
  • Append your string to a list and use the Counter method: https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item – Basile May 26 '19 at 04:49

2 Answers2

2

You are trying to do what collections.Counter does:

from collections import Counter

s = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'

print(Counter(s))

From there to get list of counts, use:

Counter(s).values()
Austin
  • 25,759
  • 4
  • 25
  • 48
0

You are basically on the right track but there's no need to split the string since Python already offers you the ability to loop through each character of the string.

A modified version of your attempt would be:

s = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
word_count_dictionary = {}

for c in s:
    if c not in word_count_dictionary:
        word_count_dictionary[c] = 1
    else:
        word_count_dictionary[c] += 1

print(word_count_dictionary)
Omari Celestine
  • 1,405
  • 1
  • 11
  • 21