list [-1]在python程序中产生“list index out of range”错误

时间:2011-08-03 16:52:20

标签: python python-2.x markov

我正在尝试构建一个马尔可夫生成器,它将字符串的任意长度作为编程练习,但我发现了一个我似乎无法修复的错误。当我运行markov函数时,我得到列表索引超出范围。

我觉得我忽略了一些明显的东西,但我不确定是什么。追溯说错误在第41行,words[-1] = nextWords[random.randint(0, len(nextWords)-1)]

完整代码如下,对不起,如果缩进被弄乱了。

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.

import random

class Markov(object):
    def __init__(self, open_file):
        self.cache = {}
    self.open_file = open_file
    open_file.seek(0)
    self.wordlist = open_file.read().split()

def get_random_list(self, length):
    i = random.randint(0, len(self.wordlist) - (length - 1))
    result = self.wordlist[i:i + length]
    return result

def find_next_word(self, words):
    candidates = []
    for i in range(len(self.wordlist) - len(words)):
        if self.wordlist[i:i + len(words)] == words and self.wordlist[i+len(words)+1] not in candidates:
            candidates.append(self.wordlist[i+len(words)+1])
    return candidates

def markov(self, length=20, chainlength=2):
    gibberish = []
    words = self.get_random_list(chainlength)
    for i in range(len(words)-1):
        gibberish.append(words[i])
    while len(gibberish) < length:
        #find candidate for next word
        nextWords = self.find_next_word(words)
        gibberish.append(words[-1])
        for i in range(len(words)):
            try:
                words[i] = words[i+1]
            except:
                pass
        words[-1] = nextWords[random.randint(0, len(nextWords)-1)]
    return " ".join(gibberish)

1 个答案:

答案 0 :(得分:7)

如果words为空,那么肯定会发生。尝试访问空数组中的words[-1]words[0]一样无效。添加一项检查以查看是否len(words) == 0。同样的逻辑适用于nextWords,在此代码中看起来也可能是空的。