4

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

Martin Schlagnitweit
  • 2,042
  • 6
  • 26
  • 42
Zak
  • 1,221
  • 2
  • 8
  • 5

2 Answers2

7
import itertools
[''.join(value) for key, value in itertools.groupby(my_str)]
Ross Patterson
  • 5,702
  • 20
  • 38
  • Thank you, that worked flawlessly. As I am still very new to python, I would have never figured this method out. I was writing a very lengthy function to do what you did in one line. Is there any chance you could explain how exactly this works, or point me in the right direction? Im looking at the itertools doc, but this is a little more than I am able to grasp ATM. – Zak Aug 31 '11 at 18:30
  • Yeah, the stuff in itertools is awesome and often mind bending. :-) See the equivalent implementation in the [itertools.groupby](http://docs.python.org/library/itertools.html#itertools.groupby) docs and I'll bet what you were writing was similar in length. The difference is that itertools does it truly iteratively and so doens't need to load the whole sequence into memory or keep it around to do this. You might want to [read up](http://www.learningpython.com/2009/02/23/iterators-iterables-and-generators-oh-my/) on iterators and why they're good. – Ross Patterson Aug 31 '11 at 18:36
3
>>> 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']
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306