Python Forum
Python Consecutive characters - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Consecutive characters (/thread-26115.html)



Python Consecutive characters - vijju56 - Apr-21-2020

If a string is given, substitute the character with number of times that character is repeated only if the character is repeated more than three times consecutively like below

Quote:Input: aaaaa Expected Output: 5Za

Input: addeeeeuyyyyy Expected OutPut: add4Zeu5Zy

Tried like below:

>>> from itertools import groupby
>>> strs="aaaaa"
>>> [[k, len(list(g))] for k, g in groupby(strs)]
[['a', 5]]
>>> 
python-3.x



RE: Python Consecutive characters - deanhystad - Apr-21-2020

Really close.
def compress(line):
    buf = ''
    for key, group in itertools.groupby(line):
        if (count := len(list(group))) > 3:
            buf += str(count) + key
        else:
            buf += key * count
    return buf
There has got to be a better way to get a count than converting to a list

Could also do without itertools
def compress(line):
    buf = ''
    count = 0
    prev = None
    for letter in line:
        if letter == prev:
            count += 1
        elif letter != prev:
            if count > 3:
                buf += str(count) + prev
            elif count > 0:
                buf += prev * count
            count = 1
            prev = letter
    return buf



RE: Python Consecutive characters - bowlofred - Apr-21-2020

(Apr-21-2020, 05:48 PM)deanhystad Wrote: There has got to be a better way to get a count than converting to a list

The only way to get the length of an iterator (assuming it has a defined length) is to consume it. So other than the memory usage, list() isn't awful.

If you don't need the data inside and wanted to roll your own, you could just keep count as you consumed elements from the iterator.

The more_itertools package has ilen(), which is very efficient at counting iterators.


RE: Python Consecutive characters - deanhystad - Apr-21-2020

I need to get over my C hoarding habits.


RE: Python Consecutive characters - TomToad - Apr-22-2020

You can do this with regular expressions
import re
regex = re.compile(r'((.)\2*)')

matches = regex.finditer("aaaabbbccddddddddeee")
s = ""
for match in matches:
    if len(match.group(0)) > 3:
        s += str(len(match.group(0)))+"Z"+match.group(0)[0]
    else:
        s += match.group(0)

print(s)