首页 > 其他 > 详细

Random words

时间:2014-08-17 19:47:42      阅读:319      评论:0      收藏:0      [点我收藏+]

To choose a random word from the histogram, the simplest algorithm is to build a list with multiple copies of each word, according to the observed frequency, and then choose from the list:

bubuko.com,布布扣                       

The expression[word] * freq creates a list with freq copies of the string word (actually, to be more precise, the elements are references to the same string). The extend method is similar to append expect that the argument is a sequence.

This algorithm works, but it is wildly inefficient; each time you choose a random word, it rebuilds the list, which is as big as the original book.

If you generate a series of words from the book, you can get a sense of the vocabulary, but it probably won’t make much sense. The next section is about generating random text that makes more sense.

 

 

from Thinking in Python

Random words,布布扣,bubuko.com

Random words

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

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