求大神帮忙python编程

[复制链接]
查看11 | 回复2 | 2021-1-27 06:42:50 | 显示全部楼层 |阅读模式


分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:42:50 | 显示全部楼层
通过正则方式想出了一个答案
做个参考

importre
input_str=input('请输入字符串:')
exist_str=set(input_str)#将字符串去重
co_len_max=0
foriinexist_str:
#通过正则查找co-string
co_list=re.findall('[{}]*{}[{}]*'.format(i,i,i),input_str)
#将每个list里的co-string提取出来,查看长度
co_str=co_list[0]
co_str_len=len(co_str)
ifco_str_len>co_len_maxandco_str_len>1:
#将长度最大的赋值给co_len_max
co_len_max=co_str_len
print(co_len_max)

回复

使用道具 举报

千问 | 2021-1-27 06:42:50 | 显示全部楼层
刚才代码不全面,同一个字符出现多次(abcaaaabbbbb)
输出会出错
优化后代码
importre
input_str=input('请输入字符串:')
exist_str=set(input_str)#将字符串去重
co_len_max=0
foriinexist_str:
#通过正则查找co-string
co_list=re.findall('[{}]*{}[{}]*'.format(i,i,i),input_str)
print(co_list)
#将每个list里的co-string提取出来,查看长度
forjinrange(len(co_list)):
co_str=co_list[j]
co_str_len=len(co_str)
ifco_str_len>co_len_maxandco_str_len>1:
#将长度最大的赋值给co_len_max
co_len_max=co_str_len
print(co_len_max)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行