【发布时间】:2012-08-11 21:51:50
【问题描述】:
我正在尝试运行 Python 初学者书籍 Think Python 中的 this 代码,以对文本文件进行马尔可夫分析。当我运行作为解决方案提供的代码时,我得到一个 IndexError: List index out of range from the 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 文件上运行它。这是从古腾堡计划下载的一本书的文本。
【问题讨论】:
-
你在哪个文本文件上运行它? (它必须以特定格式运行)。
-
哪个文本文件?你有链接吗? (它必须不符合我在下面的答案中描述的格式)。