2018年6月15日

def get_text():
    txt = open('hamlet.txt', 'r').read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
        txt = txt.replace(ch, ' ')
    return txt


hamlet_txt = get_text()
words = hamlet_txt.split()
counts = {}
for word in words:
    counts[word] = counts.get(word, 0) + 1
its = list(counts.items())
its.sort(key=lambda x: x[1], reverse=True)
for i in range(10):
    word, count = its[i]
    print("{0:<10}{1:>5}".format(word, count))

猜你喜欢

转载自www.cnblogs.com/2018jason/p/9188111.html
今日推荐