任意一个英文的纯文本文件,统计其中的单词出现的个数shell python 两种语言实现

现有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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...