Python - 合并字符串中的重复字符(ins序列)?
问题描述:
当我有一个特定的字符在一个序列中重复多次时,我想合并它,所以不会有相同的相邻字符。Python - 合并字符串中的重复字符(ins序列)?
例如:
s = 'aa.bc.a.d.aaa.z'
# after merging repeating "a" character in a sequence:
输出应该是:a.bc.a.d.a.z
P.S.我能够找到这种匹配的开始和结束索引(使用正则表达式),但仍然无法看到正常合并那些像我需要的方式。
只是为了澄清我是如何找到它:
import re
f = re.finditer('(a){2,}', s)
for match in f:
match.span()
(0, 2)
(10, 13)
答
您可以使用itertools.groupby
:
s = ''.join(ch for ch, _ in itertools.groupby('aa.bc.a.d.aaa.z'))
答
尝试:
>>> re.sub(r'(\D)\1+', r'\1', s)
'a.bc.a.d.a.z'
+1
为什么只有非数字('\ D')?看起来OP需要匹配任何角色。 –
+0
这是一个微不足道的替代 –
答
如何:
string = 'aa.bc.a.d.aaa.z'
res = ""
for c in string:
if not res.endswith(c):
res += c
这个有趣的 – Andrius