Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换Python中重复的连续字符_Python_Python 3.x - Fatal编程技术网

替换Python中重复的连续字符

替换Python中重复的连续字符,python,python-3.x,Python,Python 3.x,我需要创建一个函数,用单个字符替换重复的连续字符,例如: 'hiiii how are you??' -> 'hi how are you?' 'aahhhhhhhhhh whyyyyyy' -> 'ah why' 'foo' -> 'fo' 'oook. thesse aree enoughh examplles.' -> 'ok. these are enough examples' 使用简单的迭代 演示: def cleanText(val): r

我需要创建一个函数,用单个字符替换重复的连续字符,例如:

 'hiiii how are you??' -> 'hi how are you?'
 'aahhhhhhhhhh whyyyyyy' -> 'ah why'
 'foo' -> 'fo'
 'oook. thesse aree enoughh examplles.' -> 'ok. these are enough examples'

使用简单的迭代

演示:

def cleanText(val):
    result = []
    for i in val:
        if not result:
            result.append(i)
        else:
            if result[-1] != i:
                result.append(i)
    return "".join(result)

s = ['hiiii how are you??', 'aahhhhhhhhhh whyyyyyy', 'foo', 'oook. thesse aree enoughh examplles.']
for i in s:
    print(cleanText(i))
hi how are you?
ah why
fo
ok. these are enough examples.
输出:

def cleanText(val):
    result = []
    for i in val:
        if not result:
            result.append(i)
        else:
            if result[-1] != i:
                result.append(i)
    return "".join(result)

s = ['hiiii how are you??', 'aahhhhhhhhhh whyyyyyy', 'foo', 'oook. thesse aree enoughh examplles.']
for i in s:
    print(cleanText(i))
hi how are you?
ah why
fo
ok. these are enough examples.

您可以尝试使用类似于
()\1+
的正则表达式,即“something,then more of the same something”,并将其替换为
\1
,即“that first something”

使用
functools.partial
(或您喜欢的任何其他方式)使其成为函数


您好

解决方案可以通过以下方式非常简洁地表达:

itertools.groupby
根据给定的键函数对iterable中的对象进行分组。只要密钥相等,组就会累积。如果未给出键函数,则使用项目的标识,在本例中为字符

一旦按对象的标识对它们进行分组,就可以将对象合并到单个字符串中。分组对象作为元组返回,元组包含对象和内部
itertools.\u grouper
对象,出于您的目的,您可以忽略并提取字符

这可以转化为如下功能:

def remove_repeated_characters(s):
    groups = itertools.groupby(s)
    cleaned = ''.join(g[0] for g in groups)
    return cleaned
这将产生预期值:

>>> [remove_repeated_characters(s) 
     for s in ['hiiii how are you??','aahhhhhhhhhh whyyyyyy',
               'foo', 'oook. thesse aree enoughh examplles.']]
['hi how are you?', 'ah why', 'fo', 'ok. these are enough examples.']

嗨,圣地亚哥M。!到目前为止你试过什么?如果可能的话,请提供一些代码。如果这个问题是关于家庭作业的,请阅读这篇文章。
def remove_repeated_characters(s):
    groups = itertools.groupby(s)
    cleaned = ''.join(g[0] for g in groups)
    return cleaned
>>> [remove_repeated_characters(s) 
     for s in ['hiiii how are you??','aahhhhhhhhhh whyyyyyy',
               'foo', 'oook. thesse aree enoughh examplles.']]
['hi how are you?', 'ah why', 'fo', 'ok. these are enough examples.']
from collections import OrderedDict

def removeDupWord(word):
   return "".join(OrderedDict.fromkeys(word))

def removeDupSentence(sentence):
    words = sentence.split()
    result = ''
    return ''.join([result + removeDupWord(word) + ' ' for word in words])


sentence = 'hiiii how are you??'
print (removeDupSentence(sentence))

>>> hi how are you?