How to remove duplicates only if consecutive in a

2020-01-26 10:46发布

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.

9条回答
手持菜刀,她持情操
2楼-- · 2020-01-26 11:09

Hint: the itertools module is super-useful. One function in particular, itertools.groupby, might come in really handy here:

itertools.groupby(iterable[, key])

Make an iterator that returns consecutive keys and groups from the iterable. The key is a function computing a key value for each element. If not specified or is None, key defaults to an identity function and returns the element unchanged. Generally, the iterable needs to already be sorted on the same key function.

So since strings are iterable, what you could do is:

use groupby to collect neighbouring elements
extract the keys from the iterator returned by groupby
join the keys together

which can all be done in one clean line..

查看更多
Emotional °昔
3楼-- · 2020-01-26 11:11

+1 for groupby. Off the cuff, something like:

from itertools import groupby
def remove_dupes(arg):
    # create generator of distinct characters, ignore grouper objects
    unique = (i[0] for i in groupby(arg))
    return ''.join(unique)

Cooks for me in Python 2.7.2

查看更多
smile是对你的礼貌
4楼-- · 2020-01-26 11:13

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:

foo = '12233322155552'
bar = ''
for chr in foo:
    if bar == '' or chr != bar[len(bar)-1]:
        bar += chr

or, using the itertools hint from above:

''.join([ k[0] for k in groupby(a) ])
查看更多
登录 后发表回答