Python strings: quickly sum the number of characters in order of appearance

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']) # 5
print(counter_str1['B']) # 4
print(counter_str1['C']) # 3
print(counter_str1['D']) # 2

There also str.count(sub[, start[, end]

sub [, ]. start end .

:

print(string1.count('A'))  ## 5
+4
2

- .

def freq_map(s):
    num = 0         # number of adjacent, identical characters
    curr = s[0]     # current character being processed
    result = ''     # result of function

    for i in range(len(s)):
        if s[i] == curr:
            num += 1
        else:
            result += str(num) + curr
            curr = s[i]
            num = 1

    result += str(num) + curr

    return result

. , .

, CoryKramer . 58% . .

+1

itertools.groupby . - join, .

from itertools import groupby
def summarize(s):
    return ''.join(str(sum(1 for _ in i[1])) + i[0] for i in groupby(s))

>>> summarize(string1)
'5A4B3C2D'
>>> summarize(string2)
'2C1B1A5D1B1A1C1D1C'
>>> summarize(string3)
'1D1A1B1C1B1E1D2C1A1E1D2B'
+2

Source: https://habr.com/ru/post/1695917/


All Articles