python 统计词频

来源:互联网 发布:衣服在淘宝找不到 编辑:程序博客网 时间:2024/05/22 02:21
[^_^][22:20:46]-[~/pythonRun] vi calfreq.py


  1 #! /bin/env python
  2 # encoding=utf-8
  3 #-
  4 dic={}
  5 file=open('data.txt')
  6 for line in file.readlines():
  7     array=[]
  8     line=line.strip()
  9     array=line.split(",")
 10     for j in array:
 11         if not dic.has_key(j):
 12             dic[j]=0
 13         dic[j]+=1
 14 
 15 for i in dic.keys():
 16     print i,dic[i]
~                                                                                        
~                                                                                        
~                                                                                        
                                                                                  
                                                                       8,21          All