加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

任意一个英文的纯文本文件,统计其中的单词出现的个数(shell py

发布时间:2020-12-15 09:16:44 所属栏目:安全 来源:网络整理
导读:现有plain text titled test.txt,统计其中的单词出现的个数。 test.txt的内容: i have have application someday oneday day demo i have some one coma ideal naive i 用python实现的代码: import re count = {} f = open('test','r') b = f.read() #pri

现有plain text titled test.txt,统计其中的单词出现的个数。

test.txt的内容:

i have have application someday oneday day demo

i have some one coma ideal naive i


用python实现的代码:

import re

count = {}

f = open('test','r')

b = f.read()

#print b

cd = re.split('[ n]+',b) #注意split的用法

print cd


for i in cd:

count[i] = count.get(i,0) + 1#注意get()方法的用法

print count


执行代码后得到的结果:

['i','have','application','someday','oneday','day','demo','i','some','one','coma','ideal','naive','i']

{'someday': 1,'i': 3,'demo': 1,'naive': 1,'some': 1,'one': 1,'application': 1,'ideal': 1,'have': 3,'coma': 1,'oneday': 1,'day': 1}


shell实现的方法为:

tr " " "n"

运行结果为


1 application

1 coma

1 day

1 demo

3 have

3 i

1 ideal

1 naive

1 one

1 oneday

1 some

1 someday

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读