<分区>
我们可以通过以下方式对字符串中的连续字符进行重复数据删除:
def deduplicate(string, char):
return char.join([substring for substring in string.strip().split(char) if substring])
例如
>>> s = 'this is an irritating string with random spacing .'
>>> deduplicate(s)
'this is an irritating string with random spacing .'
在命令行上,tr
有一个squeeze
选项:
$ tr -s " " < file
Python的字符串中有squeeze
函数吗?
在 Python 中删除字符串中连续字符的最快方法是什么?
请注意,要删除重复的字符应该是任何 ascii/unicode 字符,而不仅仅是 \s
/空格。 (对于 ascii 和 unicode 有 2 个子答案很好。