Issue
I want to split an string I have by characters that are different that the others into a list. for example, if I have string ccaaawq
, I want my program to give me ['cc', 'aaa', 'w', 'q']
. Since there is no single differentiator between each split, I’m wondering what is the best approach to solving this problem.
thanks in advance for your answers
Solution
You can use itertools.groupby
:
from itertools import groupby
s = "ccaaawq"
out = ["".join(g) for _, g in groupby(s)]
print(out)
Prints:
['cc', 'aaa', 'w', 'q']
Answered By – Andrej Kesely
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0