Remove consecutive duplicate characters from a string in python

半世苍凉 提交于 2019-12-02 12:11:33

问题


Hey I was trying to write a program which will remove the consecutive duplicate characters from a string.

for example:
string->aabbccde
first iteration: bbccde
second iteration: ccde
third Iteration: de

and de is the answer.

following is the program I wrote.

a = "aabbcs"
def remove_dups(st,ind):
    print st, ind
    st = st.replace(st[ind], "")
    print st, "in dups"
    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
            remove_dups(s,ind)
    print s, 'check'        
    return s

ans = find_dups(a)
print 'answer', ans

and following is the output I am getting

aabbcs in find
6
aabbcs 0
bbcs in dups
bbcs in find
4
bbcs 0
cs in dups
cs in find
2
cs check
bbcs check
aabbcs 2
aacs in dups
aacs in find
4
aacs 0
cs in dups
cs in find
2
cs check
aacs check
aabbcs check
answer aabbcs

here above we have got cs but still answer is coming original string, I can understand it is because of recursion, but unable to understand how to resolve the issue. A little help would be appreciated. Thanks!


回答1:


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)




回答2:


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



回答3:


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



回答4:


You could easily do it using re.sub

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

OP

x



回答5:


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



回答6:


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!



来源:https://stackoverflow.com/questions/40704821/remove-consecutive-duplicate-characters-from-a-string-in-python

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