【问题标题】:index should be in range...but isn't索引应该在范围内......但不是
【发布时间】:2013-06-03 18:59:55
【问题描述】:

我正在尝试编写代码来检查是否有人在同一个句子中切换了时态。它从文本框中获取数据,将其拆分为句子,将这些句子拆分为单词,并根据不同时态的动词列表检查单词。如果句子不一致,它会突出显示它。我已经成功地做到了这一点,但是当我返回它时,我还希望在文本框中保持文本的格式相同。该程序也这样做,但是如果一个段落的第一句不一致,它将突出显示第一段和句子之间的所有空格。我尝试运行 if 语句来查找扩展空格并将其与句子分开,因此荧光笔不会将其作为句子的一部分。但是,我不断收到此错误:IndexError: list index out of range

任何帮助将不胜感激。

这里是相关代码:

def verbTense(self):#Checks sentences for inconsistent tenses
    text=self.input.get(1.0,'end')
    self.input.delete(1.0,'end')
    text=sentenceSplit(text)
    self.input.tag_config('verb',background='yellow')
    for i in text:
        if inconsistentTense(i)==True:
            self.input.insert('end',i,'verb')
        else:
            self.input.insert('end',i)

def sentenceSplit(x):#Splits a string into sentences.
    fullText=[]
    tempSentence=[]
    x=formatSplit(x)
    index=0
    for i in x:
        if i==" " and x[index+1]==" ":
            fullText.append(i)
        else:
            if ".)" in i or "!" in i or "?" in i or "." in i or "!)" in i or "?)" in i or ")" in i or "]" in i or "}" in i:
                tempSentence.append(i)
                sentence=listIntoWord(tempSentence)
                fullText.append(sentence)
                tempSentence=[]
            else:
                tempSentence.append(i)
        index+=1
    return fullText

def listIntoWord(x):#Combines list of strings into one single string.
    text=""
    for i in x:
        text+=str(i)
    return text

def formatSplit(x):#Splits a string into different words while keeping the spaces. 
    wordString=[]
    totalString=[]
    for i in x:
        if i==" ":
            wordString=listIntoWord(wordString)
            totalString.append(wordString)
            wordString=[]
            totalString.append(i)
        else:
            wordString.append(i) 
    return totalString

【问题讨论】:

  • 文字墙... tldr ;;;您的索引超出范围....请发布一个小型自包含示例来说明您的问题...
  • 请完整的堆栈跟踪,哪一行有错误?
  • 如果 i==" " 和 x[index+1]==" ":

标签: python list indexing range grammar


【解决方案1】:

您的问题在于sentenceSplit() 中的这些代码行:

index=0
for i in x:
    if i==" " and x[index+1]==" ":
        ...
    ...
    index+=1

这个循环的主体将被执行len(x) 次,在最后一次迭代中index 将是len(x)-1,所以x[index+1] 将等同于x[len(x)]。这将导致 IndexError,因为序列x 中的最后一项位于索引len(x)-1,因此len(x) 超过了序列的末尾。

要解决此问题,您可以执行以下两项操作之一:

  • 只循环到倒数第二个项目,因此当您在每次迭代中向前看时,您永远不会超过序列的结尾:

    for index, i in enumerate(x[:-1]):
        if i == " " and x[index+1] == " ":
            ...
    
  • 不要在最后一次迭代中检查下一项:

    for index, i in enumerate(x):
        if i == " " and (index == len(x)-1 or x[index+1] == " "):
            ...
    

您可以选择更适合您的代码。

请注意,我还修改了代码,使其使用enumerate(),这是循环遍历项目和索引的首选方式。

【讨论】:

    【解决方案2】:
    def sentenceSplit(x):#Splits a string into sentences.
        fullText=[]
        tempSentence=[]
        x=formatSplit(x)
        index=0
        for i in x:
            if i==" " and x[index+1]==" ":  # THIS LINE COULD BE PROBLEMATIC AT LAST ITERATION
                fullText.append(i)
    

    在循环结束时,index 将指向字符串中的最后一个字符,访问 x[index+1] 会引发 IndexError。

    【讨论】:

    • 我也是这么想的,只是我在最后添加了临时句号,它仍然突出显示了中间的空格。
    • 等待它仍然会测试它,所以我是否必须添加一个 if 语句以确保我不在迭代结束时?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2014-09-13
    • 2013-11-20
    相关资源
    最近更新 更多