获取字符串中出现次数最多的字母

#获取字符串中出现次数最多的字母
#步骤:
#1 . 遍历字符串,用str.count输出每次字符出现的次数
#2. 创建一个字典,将字母和出现次数以键值对添加到字典里
#3. 定义两个变量存储字符值,字符出现的次数
#4. 遍历字典,找出次数最大时对应的k值
def change(self):
a="aabcdsfaf"
c={}
for i in a:
num=a.count(i)
c[i]=num
print(c)

num=0
s=""
for k in c:
if c[k]>num:
num=c[k]
s=k
print("最多的字符:"+ s+" 出现次数:"+str(num) )

猜你喜欢

转载自www.cnblogs.com/menghen/p/9373611.html