一个python基础题目:
""" 题目: 设计一个函数,统计一个字符串中出现频率最高的字符及其出现次数 """ def find_most_freq(string): result_dict = {} for ch in string: if ch in result_dict: result_dict[ch] += 1 else: result_dict[ch] = 1 max_keys = [] max_value = 0 for key, value in result_dict.items(): if value > max_value: max_value = value max_keys.clear() max_keys.append(key) elif value == max_value: max_keys.append(key) return max_keys, max_value def main(): print(find_most_freq('aabbaaccbb')) # (['a', 'b'], 4) print(find_most_freq('hello, world!')) # (['l'], 3) print(find_most_freq('a1bb2ccc3aa')) # (['a', 'c'], 3) if __name__ == '__main__': main()