首页 > 其他 > 详细

Word histogram

时间:2014-08-17 15:30:32      阅读:333      评论:0      收藏:0      [点我收藏+]

Here is a program that reads a file and builds a histogram of the words in the file:

  bubuko.com,布布扣                     

process_file loops through the lines of the file, passing them one at a time to process_line. The histogram h is being used as an accumulator. process_line uses the string method replace to replace hyphens with spaces before using split to break the line into a list of strings. It traverses the list of words and uses strip and lower to remove punctuation and convert to lower case. (It is a shorthand to say that strings are ‘converted;’ remember that string are immutable, so methods like strip and lower return new strings.)

Finally, process_line updates the histogram by creating a new item incrementing an existing one. To count the total number of words in the file, we can add up the frequencies in the histogram:

 bubuko.com,布布扣

 

from Thinking in Python

Word histogram,布布扣,bubuko.com

Word histogram

原文:http://www.cnblogs.com/ryansunyu/p/3917789.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!