Python Forum
Python Consecutive characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Consecutive characters
#1
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
Reply
#2
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
Reply
#3
(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.
Reply
#4
I need to get over my C hoarding habits.
Reply
#5
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)
Reply


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

Forum Jump:

User Panel Messages

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