Remove consecutive duplicate characters from a string in python

江枫思渺然 提交于 2019-12-02 07:18:26

Your line remove_dups(s,ind) is the problem. It's not doing anything with the returned value. If you read through your code, in the top level function call you're assigning s=text at the top, then returning s at the bottom, without ever modifying the value of s. The clue is that you're printing the original text last, after you've printed the correct answer.
Try s = remove_dups(s, ind)

python have some easier way to do this, one of them:

>>> dup_string = 'aabcbccde'
>>> from itertools import groupby
>>> ''.join([x for x,y in groupby(dup_string) if sum(1 for i in y)<2])
'bcbde'
>>> dup_string = 'aabbccde'
>>> ''.join([x for x,y in groupby(dup_string) if sum(1 for i in y)<2])
'de'
>>> 

If you're going to call the find_dups method recursively, you might as well get rid of the for loop. Just remove the consecutive duplicates as soon as you find them, and then recursively call find_dups again on the newly returned string.

a = "aabbcs"
def remove_dups(st,ind):
     return st.replace(st[ind:ind+1], "")


def find_dups(text, i):
    if len(text)-1 == i:
        return text
    if(text[i]==text[i+1]):
        text = remove_dups(text,i)
        text = find_dups(text, i)
    else:
        text = find_dups(text, i+1)
    return text

ans = find_dups(a, 0)
print "answer", ans

You could easily do it using re.sub

import re
str = "aaaabbcccdddx"
print(re.sub(r"(.)\1+", '', str))

OP

x

You should be returning the values of the string, since these are passed by copies. Also once you are done with remove_dups you should break out since you are no longer interested in the same you just modified.

a = "aabbcs"
def remove_dups(st,ind):
    print st, ind
    st = st.replace(st[ind], "")
    print st, "in dups"
    return find_dups(st)

def find_dups(text):
    s=text
    print s, "in find"
    ln = len(s)
    print ln
    fg = 0
    ind = 0
    if ln==1:
        print s, 'len'
        return s
    for i in range(0,ln-1):
        if(s[i]==s[i+1]):
            ind = i
            s = remove_dups(s,ind)
            break
    print s, 'check'        
    return s
ans = find_dups(a)
print 'answer', ans

bellow is your function to do the job

def remove_duplicates(str):
  integer=0
  while integer<len(str)-1:
      if str[integer]==str[integer+1]:
          str=str.replace(str[integer]," ",2)
      integer=integer+1    
  str=str.replace(" ","")
  print(str)

you can include the print statements that i left out!

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