只有在字符串中连续时如何删除重复项?
问题描述
对于'12233322155552'这样的字符串,通过去除重复,我可以得到'1235'.
For a string such as '12233322155552', by removing the duplicates, I can get '1235'.
但我要保留的是'1232152',只删除连续的重复项.
解决方案
import re
# Only repeated numbers
answer = re.sub(r'(d)1+', r'1', '12233322155552')
# Any repeated character
answer = re.sub(r'(.)1+', r'1', '12233322155552')
相关文章