I want to split a string of 1s and 0s into a list of repeated groups of these characters.
Example:
m = '00001100011111000'
Goes to:
m = ['0000', '11', '000', '11111', '000']
How would I do this in efficient concise code? Would regex work for this?
CodePudding user response:
Yes, you can use re
. For example:
import re
m = "00001100011111000"
print(["".join(v) for v, _ in re.findall(r"((.)\2*)", m)])
Prints:
['0000', '11', '000', '11111', '000']
Other regex:
print(re.findall(r"0 |1 ", m))
Prints:
['0000', '11', '000', '11111', '000']
CodePudding user response:
You can use itertools groupby
from itertools import groupby
m = '00001100011111000'
["".join(g) for _, g in groupby(m)]
CodePudding user response:
Another method using itertools.groupby
:
from itertools import groupby
["".join(g) for k, g in groupby(m)]
CodePudding user response:
You can make use of re.finditer():
m = "00001100011111000"
[s.group(0) for s in re.finditer(r"(\d)\1*", m)]
Output:
['0000', '11', '000', '11111', '000']