开发者

How to find and split a string by repeated characters?

I am trying to split a string into a list, separated by a change in character, in python. I am finding开发者_如何转开发 this very difficult, but am sure that I am over thinking it and missing a probably simple solution. Example:

'abgg22ffeeekkkk1zzabbb'

would become:

['a', 'b', 'gg', '22', 'ff', 'eee', 'kkkk', '1', 'zz', 'a', 'bbb']


import itertools
[''.join(value) for key, value in itertools.groupby(my_str)]


>>> import re
>>> my_str = 'abgg22ffeekkkk1zzabbb'
>>> [m.group() for m in re.finditer(r'(.)\1*', my_str)]
['a', 'b', 'gg', '22', 'ff', 'ee', 'kkkk', '1', 'zz', 'a', 'bbb']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜