Skip to content Skip to sidebar Skip to footer

How To Separate A String Of Repeating Characters?

All continuous groups of characters must be grouped together and put into a list. For example, if I have this string: 1112221121 I would want to split this into a list: ['111', '2

Solution 1:

itertools.groupby does just that:

>>> from itertools import groupby
>>> [''.join(g) for _, g in groupby('1112221121')]
['111', '222', '11', '2', '1']

Post a Comment for "How To Separate A String Of Repeating Characters?"