对于'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')
这篇关于只有在字符串中连续时如何删除重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!