How to find and split a string by repeated characters?

后端 未结 2 1447
隐瞒了意图╮
隐瞒了意图╮ 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:15
    import itertools
    [''.join(value) for key, value in itertools.groupby(my_str)]
    
    0 讨论(0)
  • 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']
    
    0 讨论(0)
提交回复
热议问题