码迷,mamicode.com
首页 > 其他好文 > 详细

中英文统计

时间:2018-10-15 14:43:06      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:alt   txt   int   amp   技术分享   word   src   png   for   

第一种方法:

fo=open("sunstr.txt","r",encoding="utf-8")
sunstr=fo.read().lower()
for ch in ",.":
    sunstr.replace(ch," ")
sunstr = sunstr.split();
sunstrset = set(sunstr)

dic = {}
for i in sunstrset:
    dic[i] = sunstr.count(i)
print(dic)


for key in dic:
    print(key,dic[key]);

wcList = list(dic.items())

def tskeSecond(elem):
    return elem[1]

wcList.sort(key=tskeSecond,reverse=True)
print(wcList)

  技术分享图片

 

 

 

第二种方法:

def getTxt():
    txt = open("sunstr.txt").read()
    txt = txt.lower()
    for ch in ‘!"@#$%^&*()+,-./:;<=>?@[\\]_`~{|}‘:
        txt.replace(ch," ")
    return txt
# sunstr = sunstr.replace(","," ")

sunstr = getTxt().split()
sunstrset = set(sunstr)

dic = {}
for i in sunstrset:
    dic[i] = sunstr.count(i)
print(dic)


for key in dic:
    print(key,dic[key]);

wcList = list(dic.items())

def tskeSecond(elem):
    return elem[1]

wcList.sort(key=tskeSecond,reverse=True)
print(wcList)

  技术分享图片

 

 

 

中文统计:

import jieba
word =open("123.txt","r",encoding="utf-8").read()
 
for ch in ",。“”":
    word = word.replace(ch,"")
 
word = jieba.cut(word)
word = list(word)
print(word)
 
 
wordset =set(word)
worddic ={}
 
for i in wordset:
    worddic[i]= word.count(i)
print(worddic)
 
wcList = list(worddic.items())
wcList.sort(key=lambda x:x[1],reverse=True)
print(wcList)

  技术分享图片

 

中英文统计

标签:alt   txt   int   amp   技术分享   word   src   png   for   

原文地址:https://www.cnblogs.com/sunyubin/p/9790019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!