python 找出字符串中出现次数最多的字母

#  请大家找出s=”aabbccddxxxxffff”中 出现次数最多的字母
# 第一种方法,字典方式:
s="aabbccddxxxxffff"
count ={}
for i in set(s):
count[i]=s.count(i)
print(count)
# print(max(count.items(),key=lambda x:x[1])[0])
max_value=max(count.values())
l=[]
for k,v in count.items():
if v==max_value:
print(k) # 第二种方法 列表方式:
# encoding=utf-8
d = {'a':1,"b":2,"c":3,"d":4,"e":4}
max_value= d['a']
result = []
for k,v in d.items():
# print(k,v)
if v > max_value:
max_value =v
result = []
result.append(k)
elif v == max_value:
result.append(k) print(result) # 方法三
s="aabbccddxxxxffff"
max_s_count=0
max_s_count_letters=[]
for i in s:
if s.count(i)>max_s_count:
max_s_count_letters=[]
max_s_count=s.count(i)
if s.count(i)==max_s_count:
max_s_count_letters.append(i)
max_s_count=s.count(i)
print(list(set(max_s_count_letters)),max_s_count)
上一篇:找出数组中重复次数最多的数值


下一篇:[汇编与C语言关系]4. 结构体和联合体