看到网上的方法假如字符串中有多个出现频率最高的字符,只能返回一个,所以通过字典索引,添加到列表中的方法可以返回所有符合条件的字符。
s = "helloword"
def max_list(s):
dict_list = {}
l1 = []
for ele in s:
if ele not in dict_list:
dict_list[ele] = 1
else:
dict_list[ele] += 1
max_value = max(dict_list.values())
for k,v in dict_list.items():
if v == max_value:
l1.append(k)
return l1,max_value
print(max_list(s))
运行结果