How to find and split a string by repeated characters?

后端 未结 2 1448
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 05:34

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 missi

2条回答
  •  隐瞒了意图╮
    2020-12-21 06:29

    >>> 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']
    

提交回复
热议问题