哈姆雷特单词的排名
哈姆雷特单词的排名
代码
def getText():
txt = open("hamlet.txt","r").read()
txt = txt.lower()
for ch in '''~!@#$%^&*()_+{}|:"<>?;',./''':
txt = txt.replace(ch," ")#去噪,为什么是以空格隔开,那个是因为有些单词之间仅仅是靠一个字符来连接
return txt
hamletTxt = getText()
words = hamletTxt.split()#split返回一个列表
dict = {}
for word in words:
dict[word]=dict.get(word,0)+1#get方法的后一个参数0代表的是当找不到值的时的默认返回值
list_cnt = list(dict.items())#无序字典转有序列表,才能排序
list_cnt.sort(key = lambda x:x[1],reverse=True)#x[1]代表的是键所对应的值
for i in range(10):
print("{0:<10}{1:>5}".format(list_cnt[i][0],list_cnt[i][1]))
输出
the 1142
and 964
to 742
of 669
i 628
you 550
a 531
my 513
hamlet 470
in 450
read和没read
代码
txt = open("hamlet.txt","r").read()
print(type(txt))
txt = open("hamlet.txt", "r")
print(type(txt))
输出
<class 'str'>
<class '_io.TextIOWrapper'>
字典方法的返回类型
代码
print(type(dict.keys()))
print(type(dict.values()))
print(type(dict.items()))
print(type(dict))
输出
<class 'dict_keys'>
<class 'dict_values'>
<class 'dict_items'>
<class 'dict'>
资源
-
嵩天的书和网课