题目描述
统计一个字符串中出现次数最多字母和次数
第一种方法,字典方式:
s = input()
count ={}
for i in set(s):
count[i]=s.count(i) #字典统计次数
max_value = max(count.values())
for k,v in count.items():
if v == max_value:
print(k,max_value)
第二种方法,列表方式(不用max函数,循环得最多次数):
s = input()
max_s_count=0
max_s_count_letters=[]
for i in set(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(max_s_count_letters,max_s_count)
网友评论