媚惑江山-python统计文本字符串里单词出现频率

前端开发 用法 217浏览 116评论

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

#wordfrequencyinatext

#testedwithPython24vegaseat25aug2005

#Chinesewisdom...

str1="""Manwhoruninfrontofcar,gettired.

Manwhorunbehindcar,getexhausted."""

print"Originalstring:"

printstr1

print

#createalistofwordsseparatedatwhitespaces

wordList1=str1.split(None)

#stripanypunctuationmarksandbuildmodifiedwordlist

#startwithanemptylist

wordList2=[]

forword1inwordList1:

#lastcharacterofeachword

lastchar=word1[-1:]

#usealistofpunctuationmarks

iflastcharin[",",".","!","?",";"]:

word2=word1.rstrip(lastchar)

else:

word2=word1

#buildawordListoflowercasemodifiedwords

wordList2.append(word2.lower())

print"Wordlistcreatedfrommodifiedstring:"

printwordList2

print

#createawordfrequencydictionary

#startwithanemptydictionary

freqD2={}

forword2inwordList2:

freqD2[word2]=freqD2.get(word2,0)+1

Python实现XML文件解析的示例代码,你知道吗?2018-02-06

Pythonselenium三种等待方式2018-01-25

Python中的array数组模块相关使用2018-01-25

python写个自动SSH登录媚惑江山的小工具2018-02-07

Python中的id()函数指的什么呢?2018-01-25

利用python更新ssh远程代码操作媚惑江山的实现代码2018-02-09

python版微信跳一跳游戏辅助很厉害哦!2018-01-25

Python中的with...as用法介绍媚惑江山2018-05-11

高手在民间!Python输出PowerPoint(ppt)文件中全部文字信息的媚惑江山2017-08-31

可别小看了安装Python2017-06-19

基础教程

Python中的with...as用法介绍媚惑江山

python执行子进程实现进程间通信的好办法!

Python中操作字符串之replace()媚惑江山,值得学习!

python中管道用法入门,举个例子

python获取一组数据里最大值max函数

关于本站|下载帮助|