python统计文本字符串里单词出现频率的方法

   本文实例讲述了python统计文本字符串里单词出现频率的方法。分享给大家供大家参考。具体实现方法如下:

  ?

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

# word frequency in a text
# tested with Python24 vegaseat 25aug2005
# Chinese wisdom ...
str1 = """Man who run in front of car, get tired.
Man who run behind car, get exhausted."""
print "Original string:"
print str1
print
# create a list of words separated at whitespaces
wordList1 = str1.split(None)
# strip any punctuation marks and build modified word list
# start with an empty list
wordList2 = []
for word1 in wordList1:
# last character of each word
lastchar = word1[-1:]
# use a list of punctuation marks
if lastchar in [",", ".", "!", "?", ";"]:
word2 = word1.rstrip(lastchar)
else:
word2 = word1
# build a wordList of lower case modified words
wordList2.append(word2.lower())
print "Word list created from modified string:"
print wordList2
print
# create a wordfrequency dictionary
# start with an empty dictionary
freqD2 = {}
for word2 in wordList2:
freqD2[word2] = freqD2.get(word2, 0) + 1
# create a list of keys and sort the list
# all words are lower case already
keyList = freqD2.keys()
keyList.sort()
print "Frequency of each word in the word list (sorted):"
for key2 in keyList:
print "%-10s %d" % (key2, freqD2[key2])

  希望本文所述对大家的Python程序设计有所帮助。

时间: 2024-11-03 01:03:44

python统计文本字符串里单词出现频率的方法的相关文章

python清除字符串里非字母字符的方法

  本文实例讲述了python清除字符串里非字母字符的方法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 s = "hello world! how are you? 0" # Short version print filter(lambda c: c.isalpha(), s) # Faster version for long ASCII strings: id_tab = "".join(map(chr, xrange(256))

php解析字符串里所有URL地址的方法_php技巧

本文实例讲述了php解析字符串里所有URL地址的方法.分享给大家供大家参考.具体如下: <?php // $html = the html on the page // $current_url = the full url that the html came from //(only needed for $repath) // $repath = converts ../ and / and // urls to full valid urls function pageLinks($ht

php字符串按照单词进行反转的方法

 本文实例讲述了php字符串按照单词进行反转的方法.分享给大家供大家参考.具体分析如下: 下面的php代码可以将字符串按照单词进行反转输出,实际上市现将字符串按照空格分隔到数组,然后对数组进行反转输出 <?php $s = "Reversing a string by word"; // break the string up into words $words = explode(' ',$s); // reverse the array of words $words = a

php字符串按照单词进行反转的方法_php技巧

本文实例讲述了php字符串按照单词进行反转的方法.分享给大家供大家参考.具体分析如下: 下面的php代码可以将字符串按照单词进行反转输出,实际上市现将字符串按照空格分隔到数组,然后对数组进行反转输出 <?php $s = "Reversing a string by word"; // break the string up into words $words = explode(' ',$s); // reverse the array of words $words = ar

【Python学习】字符串按单词反转

第一天学python,做一个作业 题目:字符串按单词反转(必须保留所有空格).'I love china!'  转化为 'china! love I' import string s = 'I love china!' s1=list(s.split()) #构建一个空数组 x=[] #反向遍历数组 for c in reversed(s1): x.append(c) #按空格连接字符串 print(' '.join(x)) 问题来了,用split()进行分割会存在问题,没法保留空格,好吧,那看

python处理文本字符串例子

需求: 对一个配置文件进行处理,拿出可用的字符来拼接,下面是原始文本,我们要得到这样的结果, redis -h 127.0.0.1 -p 6379 | select 2 redis -h 127.0.0.1 -p 6379 | select 16 redis -h 127.0.0.1 -p 6379 | select 8 原始文本:  代码如下 复制代码 PHP     'redis_list' => array(         'normal' => array(            

php解析字符串里所有URL地址的方法

 具体如下: 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 39 40 41 42 43 44 45 46 47 <?php // $html = the html on the page // $current_url = the full url that the html came from //(only needed for

python清除字符串里非数字字符的方法

  本文实例讲述了python清除字符串里非数字字符的方法.分享给大家供大家参考.具体如下: ? 1 2 3 4 import re s = "how19 a*re 254y**ou?" # Using regular expressions print re.sub("D", "", s) 希望本文所述对大家的Python程序设计有所帮助.

python统计文本文件内单词数量的方法

  本文实例讲述了python统计文本文件内单词数量的方法.分享给大家供大家参考.具体实现方法如下: ? 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 # count lines, sentences, and words of a text file # set all the counters to zero lines, bla