LetsChangeTheWorld LetsChangeTheWorld - 11 months ago 82
Python Question

Python Replace consecutive letters with different variables

I hopefully have an easy question. I just cant figure out the correct function to use. I want to replace repeating characters with different variables depending on how many times it repeats consecutively.

with open("text1.txt","r") as File:

for line in File:
Counting = line.count('a')
if Counting == 1:
Line1 = line.replace('a', '1')
elif Counting == 2:
Line1 = line.replace('aa', '2')

so if 'a' repeats consecutively 3 times I want to replace the 'aaa' with 3 and so on up to 9.The problem is, counting them counts regardless if its consecutive or not. If I read a line 2 characters or three at a time it chops it up. Any ideas or help please.

Answer Source

If you want to analyze/replace consecutive groups of letters, then perhaps itertools.groupby could be of interest. The example below first extracts all consecutive groups and then checks if the unique element in a particular group is a. If so, it replaces this group with the corresponding count of elements, otherwise, it keeps the original input.

from itertools import groupby

s = 'aaabaacdd' #test input

ret = ''
for k, v in groupby(s):
    chunk = list(v)
    cnt = len(chunk)

    if k == 'a': #the condition can be extended here, e.g., k == 'a' and cnt <= 9
        #substitute the group of 'a's with something else
        #the substitution can take into account the number of consecutive
        #'a's stored in the variable cnt
        el = '%d' % (cnt)
        el = ''.join(chunk)
    ret += el


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download