Python Forum
Python Consecutive characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Consecutive characters
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]]
Really close.
def compress(line):
    buf = ''
    for key, group in itertools.groupby(line):
        if (count := len(list(group))) > 3:
            buf += str(count) + key
            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
(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.
I need to get over my C hoarding habits.
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( > 3:
        s += str(len("Z"[0]
        s +=


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract continuous numeric characters from a string in Python Robotguy 2 1,037 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 1,346 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  Remove escape characters / Unicode characters from string DreamingInsanity 5 5,339 May-15-2020, 01:37 PM
Last Post: snippsat
  Assign the sum of 2 consecutive numbers in a list to a varibale Fenaz 3 1,098 Apr-05-2020, 10:30 PM
Last Post: bowlofred
  creating consecutive rows PyPy 1 777 Feb-07-2020, 06:46 PM
Last Post: micseydel
  Slicing Python list of strings into individual characters Drone4four 5 1,948 Apr-17-2019, 07:22 AM
Last Post: perfringo
  problems with python script and special characters last08 1 1,344 Mar-29-2019, 09:28 AM
Last Post: Kebap
  modify the color a string of characters in python 3.6 atlass218 10 3,296 Feb-28-2019, 03:20 PM
Last Post: atlass218
  Replace characters from file in Python 2.7 melmouja 2 1,520 Feb-04-2019, 01:32 PM
Last Post: melmouja
  I need to add consecutive numbers help juniorcoder 1 1,280 Sep-18-2018, 01:01 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020