仅当在字符串中连续时如何删除重复项? [英] How to remove duplicates only if consecutive in a string?

查看:100
本文介绍了仅当在字符串中连续时如何删除重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于诸如'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')

这篇关于仅当在字符串中连续时如何删除重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆