码迷,mamicode.com
首页 > 其他好文 > 详细

Random words

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

标签:style   blog   http   os   io   ar   2014   cti   

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

标签:style   blog   http   os   io   ar   2014   cti   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!