Let's say I have the following lines in Python3.x
string1 = 'AAAAABBBBCCCDD'
string2 = 'CCBADDDDDBACDC'
string3 = 'DABCBEDCCAEDBB'
I would like to create a summary “frequency string” that counts the number of characters in a string in the following format:
string1_freq = '5A4B3C2D' ## 5 A's, followed by 4 B's, 3 C's, and 2D's
string2_freq = '2C1B1A5D1B1A1C1D1C'
string3_freq = '1D1A1B1C1B1E1D2C1A1E1D2B'
My problem:
How can I quickly create such a summary string?
My idea is to create an empty list to track the score. Then create a for loop that checks the next character. If there is a match, increase the number by +1 and go to the next character. Otherwise, add 'count' + 'character identity' to the end of the line.
This is very inefficient in Python. Is there a faster way (maybe using the functions below)?
There are several ways to count string elements in python. I like collections.Counterfor example
from collections import Counter
counter_str1 = Counter(string1)
print(counter_str1['A'])
print(counter_str1['B'])
print(counter_str1['C'])
print(counter_str1['D'])
There also str.count(sub[, start[, end]
sub [, ]. start end .
:
print(string1.count('A'))