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
- github小练习004
- github小练习002
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 小练习
- 理解神经网络
- 【理解】 有向图强连通分量 Tarjan算法
- Java Web防止表单重复提交
- UVa 572 Oil Deposits
- ZOJ 3946-Highway Project【最短路的应用】(2016浙江省大学生程序设计竞赛)
- github小练习004
- [leetcode] 145. Binary Tree Postorder Traversal
- linux环境jdk安装及配置
- linux常用命令
- IaaS, PaaS和SaaS公司都做些什么
- JAVA BigDecimal 小数处理
- hdu 5670
- quartz定时任务时间设置
- JNI:基本数据类型转换