天天看点

对哈姆雷特进行词频统计

def getText():

    txt = open("C:/Users/Administrator/Desktop/python-lianxi/hamlet.txt", "r").read()

    txt = txt.lower()   #首先把所有字母都转换成小写字母

    for ch in  '!"#$%()*+,-./:;<=>?@[\\]^_{|}·~‘’':   #排除掉英文字符,用空格替换

        txt = txt.replace(ch, " ")

    return txt

hamletTxt = getText()

words = hamletTxt.split()   #通过split函数用空格进行拆分

counts = {}

for word in words:

    counts[word] = counts.get(word,0) + 1   #字典的get方法,查找是否有键word,有则返回其对应键值,没有则返回后面的值0

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))

Py