For a string such as '12233322155552'
, by removing the duplicates, I can get '1235'
.
But what I want to keep is '1232152'
, only removing the consecutive duplicates.
For a string such as '12233322155552'
, by removing the duplicates, I can get '1235'
.
But what I want to keep is '1232152'
, only removing the consecutive duplicates.
Hint: the itertools module is super-useful. One function in particular, itertools.groupby, might come in really handy here:
So since strings are iterable, what you could do is:
which can all be done in one clean line..
+1 for groupby. Off the cuff, something like:
Cooks for me in Python 2.7.2
First of all, you can't remove anything from a string in Python (google "Python immutable string" if this is not clear).
M first approach would be:
or, using the itertools hint from above: