第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数

来源:互联网 发布:五线胆码的算法 编辑:程序博客网 时间:2024/06/06 01:25
import osfile = open('wz.txt',mode='r')dict = {}for line in file:    h_line = line.split()    for key in h_line:        if (key[-1]>='a' and key[-1] <='z') or (key[-1]>='A' and key[-1] <= 'Z'):            key = key.lower()        else:            key = key.lower()[0:-1]        if key not in dict:            dict[key] = 1        else:            dict[key] += 1count = 0for key,value in dict.items():    count += valueprint(count)print(dict)
阅读全文
0 0
原创粉丝点击