1.用户输入一个英文句子。
2.打印出每个字符及其重复的次数。
3.将字符及其出现次数用字典表示,并按字符出现频率的降序打印。
1.(删去不必要的原素,replace),按照空格进行分割得到单词列表
2.字典作为数据储存(映射)
3.判断次数并排列(列表sort方法)
供楼主参考采纳!
from collections import defaultdict
target = input("请输入一个英文句子:")
target = target.replace(', ', ' ').replace('.', '')
target_list = target.split(' ')
count_dict = defaultdict(int)
for i in target_list:
count_dict[i] += 1
print(count_dict)
for item in sorted(count_dict.items(), key=lambda x: x[1], reverse=True):
print(item)