确实有段时间没怎么写python,手写还不上机是真的难受。
而且break 跳出循环最内一层的事情都要想一下才能写得出来。
题目如下:
寻找一个字符串最大连续出现次数,并放入字典中,
s="aaccccababbbb"{'a': 2, 'b': 4, 'c': 4}
代码如下
s="aaccccvbbbbbbbbbdddddddddeeedeeeeettt"dict1={}
ls=[i for i ins]for i inset(ls):
dict1[i]=1
print(dict1)for i inrange(len(ls)):
count=1
for j in range(i+1,len(ls)):if ls[i]==ls[j]:
count+= 1
if dict1.get(ls[i]) <=count:
dict1[ls[i]]=countelse:pass
else:break
print(dict1)#
#{'c': 5, 'e': 6, 'v': 2, 'b': 10, 'd': 10, 'a': 3, 't': 4}#{'c': 5, 'b': 10, 't': 4, 'a': 3, 'd': 10, 'v': 2, 'e': 6}
join 打错哦了
str=>list,set 可以直接list() set()方法转换
ls="aaccccvbbbbbbbbbdddddddddeeedeeeeettt"dict1={}for i inset(ls):
dict1[i]=1
print(dict1)for i inrange(len(ls)):
count= 1
for j in range(i+1,len(ls)):if ls[i] ==ls[j]:
count+= 1
if dict1.get(ls[i]) <=count:
dict1[ls[i]]=countelse:break
print(dict1)