github小练习004

来源:互联网 发布:电脑屏幕自动截图软件 编辑:程序博客网 时间:2024/06/05 04:23

因为003联系涉及其他数据库,所以就没有做了。

004:统计文件中单词出现的次数


先附上统计文件

In the latest move to support the economy, Shanghai, Beijing, Chongqing and six other provinces and municipalities will allow banks to refinance high-quality credit assets rated by the People's Bank of China, said the central bank, as the program was first introduced in Guangdong and Shandong provinces last year.


一.我的代码:

# -*- coding: utf-8 -*-# 自己import rewith open('ceshi.txt','r') as f:    a = []    count = {}    for i in f.read().split():        i = re.sub('\.|"|,','',i) # 正则匹配        a.append(i) # 将匹配好的数据加入到列表中    for j in a:        if count.has_key(j):   # 判断字典中是否已经存在,是---value+1,不是则添加一个元素            count[j] += 1        else:            count[j] = 1# 将结果加入到txt文件中    with open('jilu.txt','w') as f2:        for x in count:            f2.write(x + ':' + str(count[x]) + '\n')


二.作者提供的代码:

-*- coding: utf-8 -*-答案from collections import Counterimport redef creat_list(filename):    datalist = []    with open(filename, 'r') as f:        for line in f:            content = re.sub("\"|,|\.", "", line)            datalist.extend(content.strip().split(' '))    return datalistdef wc(filename):    print Counter(creat_list(filename))if __name__ == "__main__":    filename = 'ceshi.txt'    wc(filename)

结果是一样的,细节大家自己分析哈。拜拜



0 0
原创粉丝点击