好久没更新了。这段时间忙完期末又开始忙期中了……
python小白,老师布置的作业是多练习列表,字典,元组和集合。
于是乎,我照着书用字典实现了中英文词频统计。下面这个代码是我自由发挥的浙江高考英语完型词频统计。不过,统计出来的词都好简单哦哈哈哈~
第一次用计算机来解决我实际生活中的小问题,开心,纪念下。
等我会做网页了把txt文件po上来,有空我也会迭代新版本,欢迎提出优化意见。
#字典-带排除的浙江高考完型词频统计
#zjgkwx.py
excludes = {"b","c","d","from","the","and","of","you","a","i","my","in","an","me","we","our","they","us","their","you","your","he","his","her","she","it","its","this","that",
"those","these","there","here","who","where","what","which","when","how","because","however","but","or","so","time","with","is","as","out","had","up","at","to","was","for","on","have","be","him"}
def getText():
txt = open("zjgkwx.txt","r",encoding='utf-8').read()
txt = txt.lower()
for ch in '——0123456789!\'"#$%&()*+,-./:;<=>?@[\\]^_`{|}~':
txt = txt.replace(ch, " ") #将文本中特殊字符替换为空格
return txt
zjgkTxt = getText()
words = zjgkTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1
for word in excludes:
del(counts[word])
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(800):
word, count = items[i]
print("{0:<800}{1:>5}".format(word, count))
强推Python入门书《Python语言程序设计基础 第2版》,主要是有实例,边学边实践,非常有意思!