【问题标题】:Remove consecutive duplicate characters from a string in python从python中的字符串中删除连续的重复字符
【发布时间】:2017-04-03 22:36:35
【问题描述】:

嘿,我正在尝试编写一个程序,该程序将从字符串中删除连续的重复字符。

例如:
字符串->aabbccde
第一次迭代:bbccde
第二次迭代:ccde
第三次迭代:de

de 就是答案。

下面是我写的程序。

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

以下是我得到的输出

查找中的 aabbcs
6
aabbcs 0
英国广播公司在重复
英国广播公司在查找
4
英国广播公司 0
重复中的cs
查找中的 cs
2
cs检查
英国广播公司检查
aabbcs 2
重复中的 aacs
查找中的 aacs
4
aacs 0
重复中的cs
查找中的 cs
2
cs检查
aacs 检查
aabbcs 检查
回答 aabbcs

在上面我们得到了 cs 但仍然是原始字符串的答案,我可以理解这是因为递归,但无法理解如何解决问题。一点帮助将不胜感激。谢谢!

【问题讨论】:

    标签: python string duplicates


    【解决方案1】:

    python 有一些更简单的方法可以做到这一点,其中之一:

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

    【讨论】:

    • 我怀疑以ded 结尾的字符串需要ded 的输出,因为没有立即连续的字母。这将输出de 仍然......并且abcbca 将返回一个空字符串,并且根本没有任何连续......
    • 这次应该没问题。
    【解决方案2】:

    如果您要递归调用find_dups 方法,您不妨去掉 for 循环。只要找到连续的重复项就删除它们,然后在新返回的字符串上再次递归调用find_dups

    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
    

    【讨论】:

      【解决方案3】:

      你的remove_dups(s,ind) 是问题所在。它没有对返回的值做任何事情。如果您通读代码,在顶级函数调用中,您将在顶部分配s=text,然后在底部返回s,而无需修改s 的值。线索是您在打印正确答案后最后打印原始文本。
      试试s = remove_dups(s, ind)

      【讨论】:

      • 谢谢我添加了s = remove_dups(s,ind),这个s = find_dups(s) 休息了
      【解决方案4】:

      您可以使用re.sub 轻松做到这一点

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

      OP

      x
      

      【讨论】:

      • 几乎 - 您希望 '' 作为替换字符串,因此输出与 OP 期望的 de 匹配...
      【解决方案5】:

      您应该返回字符串的值,因为这些是通过副本传递的。同样,一旦你完成了remove_dups,你应该休息一下,因为你不再对刚刚修改的内容感兴趣。

      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】:

        下面是你的工作职责

        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)
        

        你可以包含我遗漏的打印语句!

        【讨论】:

          【解决方案7】:

          对于一般列表:

          mylist = [['a'], ['a'], ['a'], ['b'], ['b'], ['c'], ['d'], ['e'], ['f'], ['a'], ['a']]
          idx = 0
          while True:
              if idx == len(mylist) - 1:
                   break
              if mylist[idx] == mylist[idx + 1]:
                   del mylist[idx + 1]
                   idx = idx
              else:
                   idx = idx + 1
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-09-02
            • 2018-08-05
            • 2018-10-21
            • 1970-01-01
            • 2015-01-26
            • 1970-01-01
            • 2019-11-29
            • 1970-01-01
            相关资源
            最近更新 更多