def getText():
txt = open("E:\python学习\python_work\hamlet.txt","r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch," ")
return txt
hamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0)+1
'''刚开始字典counts是空的,所以words列表里面的任何字符,都不在counts字典里,因此第一次索引到的字符,都是不存在,为0+1,
例如a首次遇到,则为a:1,当首次添加字符到字典后,第二次再遇到相同字符时,值1+1=2,为a:2,并将2赋值给counts[a]'''
items = list (counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))