Python 为什么我从随机选择函数中得到索引器?
我试图运行python初学者书籍ThinkPython中的代码,以便对文本文件进行马尔可夫分析。当我运行作为解决方案提供的代码时,我从random.py模块得到一个indexer:List索引超出范围。我需要改变什么 代码应该是的练习8的答案,但我无法让它工作 回溯:Python 为什么我从随机选择函数中得到索引器?,python,random,markov,Python,Random,Markov,我试图运行python初学者书籍ThinkPython中的代码,以便对文本文件进行马尔可夫分析。当我运行作为解决方案提供的代码时,我从random.py模块得到一个indexer:List索引超出范围。我需要改变什么 代码应该是的练习8的答案,但我无法让它工作 回溯: Traceback (most recent call last): File "test.py", line 115, in <module> main(*sys.argv) File "test.p
Traceback (most recent call last):
File "test.py", line 115, in <module>
main(*sys.argv)
File "test.py", line 111, in main
random_text(n)
File "test.py", line 76, in random_text
start = random.choice(suffix_map.keys())
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/random.py", line 274, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
回溯(最近一次呼叫最后一次):
文件“test.py”,第115行,在
主(*sys.argv)
文件“test.py”,第111行,在main中
随机文本(n)
文件“test.py”,第76行,随机文本
start=random.choice(后缀\u map.keys())
文件“/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/random.py”,选择第274行
返回seq[int(self.random()*len(seq))]#如果seq为空,则引发索引器
编辑:我在一个.txt文件上运行它。这是从古腾堡项目下载的一本书的文本。运行程序的文件没有古腾堡头。它需要有一行以以下内容开头:
*END*THE SMALL PRINT!
将只读取其后的文本(这发生在skip_gutenberg_header
函数中)。例如,在文件上运行代码
Header info
*END*THE SMALL PRINT!
He was very clever, be it sweetness or be angry, ashamed or only amused, at such a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself.
产生(例如):
你在哪个文本文件上运行它?(必须在具有特定格式的文件上运行)。哪个文本文件?你有链接吗?(它一定不能符合我在下面的回答中描述的格式)。你确定你把这一行完全按照文本中显示的那样放了吗?尝试注释调用
skip_gutenberg_header
的行。我注释掉了对skip_gutenberg_header的调用,现在它可以工作了。因此,该函数一定有问题。是否有地方可以下载该文件?是否可能您没有从行的开头粘贴到标题行,或者在运行Python文件之前没有保存该文件?如果头信号文件在那里,它应该可以工作。另外:那一行不是文件中的最后一行,是吗?那只是头球的末端,对吗?
a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. at such a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. cannot make speeches, Emma:" he soon cut it all himself. be it sweetness or be angry, ashamed or only amused, at such a stroke. She had never thought of