I'm looking for an efficient way to chance a string such that all sequences of more than 2 equal characters are cut off after the first 2.
Some input->output examples are:
hellooooooooo -> helloo
woooohhooooo -> woohhoo
I'm currently looping over the characters, but it's a bit slow. Does anyone have another solution (regexp or something else)
EDIT: current code:
word_new = ""
for i in range(0,len(word)-2):
if not word[i] == word[i+1] == word[i+2]:
word_new = word_new+word[i]
for i in range(len(word)-2,len(word)):
word_new = word_new + word[i]
Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.
Delete multiple characters from string using filter() and join() In Python, you can use the filter() function to filter all the occurences of a characters from a string.
In Python you can use the replace() and translate() methods to specify which characters you want to remove from the string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable.
In python, we can select characters in a string using negative indexing too. Last character in string has index -1 and it will keep on decreasing till we reach the start of the string. So to remove last 3 characters from a string select character from 0 i.e. to -3 i.e.
Edit: after applying helpful comments
import re
def ReplaceThreeOrMore(s):
# pattern to look for three or more repetitions of any character, including
# newlines.
pattern = re.compile(r"(.)\1{2,}", re.DOTALL)
return pattern.sub(r"\1\1", s)
(original response here) Try something like this:
import re
# look for a character followed by at least one repetition of itself.
pattern = re.compile(r"(\w)\1+")
# a function to perform the substitution we need:
def repl(matchObj):
char = matchObj.group(1)
return "%s%s" % (char, char)
>>> pattern.sub(repl, "Foooooooooootball")
'Football'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With