综合练习:词频统计
f = open('C:\\Users\\Administrator\\Desktop\\14.txt', 'r', encoding='utf-8') a = f.read() d = {} h = '''.'!?:,''' D = ['the', 'and', 'a'] for j in h: b = set(a.replace("\'", "").lower().split()) for v in b: d[v] = a.count(v) for ec in D: d.pop(ec) e = list(d.items()) e.sort(key=lambda x: x[1], reverse=True) for k,v in enumerate(e): if k < 20: print(v)