Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split a string by character sets that are different in python

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading