Splitting a string with repeated characters into a list

I am not well experienced with Regex but I have been reading a lot about it. Assume there’s a string s = '111234' I want a list with the string split into L = ['111', '2', '3', '4']. My approach was to make a group checking if it’s a digit or not and then check for a repetition of the group. Something like this

L = re.findall('d[1+]', s)

I think that d[1+] will basically check for either “digit” or “digit +” the same repetitions. I think this might do what I want.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use re.finditer():

>>> s='111234'
>>> [m.group(0) for m in re.finditer(r"(d)1*", s)]
['111', '2', '3', '4']

Method 2

If you want to group all the repeated characters, then you can also use itertools.groupby, like this

from itertools import groupby
print ["".join(grp) for num, grp in groupby('111234')]
# ['111', '2', '3', '4']

If you want to make sure that you want only digits, then

print ["".join(grp) for num, grp in groupby('111aaa234') if num.isdigit()]
# ['111', '2', '3', '4']

Method 3

Try this one:

s = '111234'

l = re.findall(r'((.)2*)', s)
## it this stage i have [('111', '1'), ('2', '2'), ('3', '3'), ('4', '4')] in l

## now I am keeping only the first value from the tuple of each list
lst = [x[0] for x in l]

print lst

output:

['111', '2', '3', '4']

Method 4

If you don’t want to use any libraries then here’s the code:

s = "AACBCAAB"
L = []
temp = s[0]
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        temp += s[i]
    else:
        L.append(temp)
        temp = s[i]
    if i == len(s)-1:
        L.append(temp)
print(L)

Output:

['AA', 'C', 'B', 'C', 'AA', 'B']


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x