How to remove duplicates only if consecutive in a string? [duplicate]

拟墨画扇 提交于 2019-11-26 12:30:54

问题


This question already has an answer here:

  • Removing elements that have consecutive duplicates 4 answers

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.


回答1:


Microsoft / Amazon job interview type of question: This is the pseudocode, the actual code is left as exercise.

for each char in the string do:
   if the current char is equal to the next char:
      delete next char
   else
     continue

return string

As a more high level, try (not actually the implementation):

for s in string:
  if s == s+1:  ## check until the end of the string
     delete s+1



回答2:


You can use itertools, here is the one liner

>>> s = '12233322155552'
>>> ''.join(i for i, _ in itertools.groupby(s))
'1232152'



回答3:


import re

# Only repeated numbers
answer = re.sub(r'(\d)\1+', r'\1', '12233322155552')

# Any repeated character
answer = re.sub(r'(.)\1+', r'\1', '12233322155552')



回答4:


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..




回答5:


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



回答6:


+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




回答7:


number = '12233322155552'
temp_list = []


for item in number:   
   if len(temp_list) == 0:
      temp_list.append(item)

   elif len(temp_list) > 0:
      if  temp_list[-1] != item:
          temp_list.append(item)

print(''.join(temp_list))



回答8:


This would be a way:

def fix(a):
    list = []

    for element in a:
        # fill the list if the list is empty
        if len(list) == 0:list.append(element)
        # check with the last element of the list
        if list[-1] != element:  list.append(element)

    print(''.join(list))    


a= 'GGGGiiiiniiiGinnaaaaaProtijayi'
fix(a)
# output => GiniGinaProtijayi



回答9:


t = '12233322155552'
for i in t:
    dup = i+i
    t = re.sub(dup, i, t)

You can get final output as 1232152



来源:https://stackoverflow.com/questions/11460855/how-to-remove-duplicates-only-if-consecutive-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!