统计个数
来源:互联网 发布:淘宝怎么查看收获地址 编辑:程序博客网 时间:2024/05/03 22:01
in 文件每行有三列,tab分隔,统计最后一列每个字符出现的字数,其实是数字,但是当作字符处理。没什么问题,主要是最后想要对dict 按key值排序出了问题,python3 的dict不能用sort 什么的,最后也只能搞成按字符排序,而不是按key的数值排序。
摘抄一段python 文档
使用collections 里的 OrderedDict() ,写的时候试了下,把key值转换成int 来compare,ok,好丑上
import collectionsfile=open('filename.txt','r')count={}f1=open('c3.txt','w')for lines in file.readlines():lines=lines.rstrip('\n')line=lines.split('\t')[2]if line in count:count[line]+=1else:count[line]=1f=open('out.txt','w')count=collections.OrderedDict(sorted(count.items(), key=lambda t: int(t[0])))for i in count:i=str(i)count[i]=str(count[i])f.write(i+'\t'+count[i]+'\n')f.close
0 0
- 统计个数
- 统计个数
- 统计个数
- 字段个数统计
- 统计邮件个数(hotmail)
- 统计正负数个数
- 统计字符个数!!!
- 统计中文个数
- vc 统计单词个数
- 文件个数、文件夹统计
- 统计字符个数
- 文件个数、文件夹统计
- 统计字符个数
- 统计单词个数
- 统计单词个数
- 统计产生数字个数
- js统计元素个数
- 双色球个数统计系统
- HDU 4707 Pet
- spark原理
- vector的使用方法
- 线程的几个有意思的方法
- lightoj 1037 - Agent 47 (状压DP)
- 统计个数
- 李刚疯狂讲义目录
- React 实例-深入理解
- Introduce to algorithm--------pseudo code to C/C++ code (chapter 18)
- static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
- upyun结合carriwave实现图片的云同步
- phthon基础
- busybox详解
- R语言中的rgl包的安装问题