Python统计字符串子串重复次数

 时间:2024-10-12 08:06:16

1、遍历字符串所有子串,并存于字典中,每一个子串,在字典中寻找,如果存在,key加一,否则新加入key,赋值为1。dic={}s='AAAA'for i in range(len(s)): for j in range(i+2,len(s)): t=s[i:j] if t in dic: dic[t]+=1 else: dic[t]=1

Python统计字符串子串重复次数
Python统计字符串子串重复次数
Python统计字符串子串重复次数

3、这个方法我把它叫做蠕斗析翡那虫。因为他是无重复统计,类似于蠕虫的运动。获取所有字符,并统计在字符串中出现的次数。def worm(s): dic={} for i in range(len烫喇霰嘴(s)-3): j=i s1=s[i:i+2] s2=s[i+2:] while s2.find(s1)!=-1: count=1 stemp=s2 while stemp.find(s1)!=-1: count+=1 stemp=stemp[stemp.find(s1)+len(s1):] if not(s1 in dic): dic[s1]=count j+=1 s1=s[i:j+2] s2=s[j+2:] return dic

4、这是一个升级版的方法,因为它在比较之前引入了查询,避免不必要的统计希望各位生物信息工作者可以相互交流,提出修改建议。def worm(s): dic={} for i in range(len(s)-3): j=i s1=s[i:i+2] if s1 in dic: continue s2=s[i+2:] while s2.find(s1)!=-1: count=1 stemp=s2 while stemp.find(s1)!=-1: count+=1 stemp=stemp[stemp.find(s1)+len(s1):] if not(s1 in dic): dic[s1]=count j+=1 s1=s[i:j+2] s2=s[j+2:] return dic

  • 团油如何开启车牌支付?
  • 过年该回哪个家?婆家还是娘家?
  • 红豆小米糕怎么做简单又好吃?
  • 孕晚期喝玉米须水有什么作用
  • 叉烧包的做法
  • 热门搜索
    儿童手抄报模板 全国学生营养日手抄报 关于健康的手抄报内容 关于名著的手抄报 三年级上册手抄报 法制伴我行手抄报 我们的节日中秋节手抄报 关于我爱读书手抄报 阅读手抄报的内容 有关弟子规的手抄报