题目二十二
编写一个程序来计算输入单词的频率。输出应在对键进行字母数字排序后输出。
假设向程序提供了以下输入:
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
输出为:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1
代码实现
方法一
word = input("请输入需要统计的内容:").split()
"""
这里主要涉及两个知识点:
1.set()函数能够将其他类型转换成集合类型,并去掉重复元素
2.sorted()函数,能够在保留原列表的基础上,新建一个已经排序的列表
"""
lst = sorted(set(word))
for i in lst:
print("{0},{1}".format(i,word.count(i)))
方法二
word = input("请输入需要统计的内容:").split()
di