簡體   English   中英

為什么我從隨機選擇函數中得到IndexError?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM