[英]Why am I getting an IndexError from a random choice function?
我正在尝试从初学者的python书中运行此代码,以Think Python在文本文件上进行马尔可夫分析。 当我运行作为解决方案提供的代码时,出现IndexError:List索引超出random.py模块的范围。 我需要更改什么?
该代码应该是Think Python练习8的答案,但我无法使其正常工作。
追溯:
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
编辑:我正在.txt文件上运行它。 这是从古腾堡计划(Project Gutenburg)下载的一本书的文字。
您正在运行程序的文件没有Gutenberg标头。 它需要以下列开头的行:
*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.
产生(作为一个例子):
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.