s='AAAAHHHBBCDHHHH' # s='AABAAA' def sd(s): k = [] rs = [] for i in s: # 如果i改变了,就把之前的数据存储,重新计数 if i not in k: if k: rs.append([len(k), k[0]]) # print(len(k), k[0], k, i) k = [] k.append(i) else: k.append(i) rs.append([len(k),k[0]]) return rs print('分组结果:',sd(s)) sd=[s for s in sd(s) if s[0] >2] if len(sd)==1: print(1) if len(sd) ==2: if sd[0][1]==sd[1][1]: print(2) else: print(1) dis={} if len(sd) >2: for x in sd: if x[1] not in dis.keys(): dis[x[1]]=1 else: dis[x[1]]+=1 lds=list(dis.values()) lds.sort(reverse=True) # lds.sort(key=lambda x:x[1],reverse=True) print(max(lds)) if len(sd)<1: print(-1)