简体   繁体   English

IndexError:我的函数的字符串索引超出范围

[英]IndexError: string index out of range on my function

I am trying to create a function which allows me to split up a string and add each word to a list then return the words which begin with a certain letter in that list without using the .split() command. 我正在尝试创建一个函数,该函数使我可以拆分一个字符串并将每个单词添加到列表中,然后不使用.split()命令而返回该列表中以某个字母开头的单词。 The first part of the function (splitting the string and adding each word to a list) works perfectly fine. 函数的第一部分(将字符串拆分并将每个单词添加到列表中)工作得很好。 The issue is when trying to return the values within that list which begin with a certain letter. 问题是当尝试返回该列表中以某个字母开头的值时。 Here is my code: 这是我的代码:

def getWordsStartingWith(text, letter):
    split_text = [] #This is where each word is appeneded to.
    text_addition = ""  #This is where the letters from the string are added.
    number = 0
    gWSW = []
    for str in text:
        if str == ' ' or str == "'": # Checks to see whether the letter is a space or apostrophy.
            split_text.append(text_addition)
            text_addition = "" #If there is, then the letters collected so far in text_addition are apended to the list split_text and then cleared from text_addition
        else:
            text_addition += str #If not, then the letter is added to the string text_addition.

    while number < len(split_text)-1:
        if split_text[number][0] == letter:
            gWSW.append(split_text[number])
            number += 1
        else:
            number += 1
    else:
        return gWSW

The issue is with the line 问题在于线

if split_text[number][0] == letter: 如果split_text [number] [0] ==字母:

where it returns an IndexError as stated in the title. 如标题中所述返回IndexError。 I'm pretty sure it has something to do with the [number] variable being used but not sure what to do. 我很确定它与正在使用的[number]变量有关,但不确定该怎么做。

Like the comments to your question states you have a couple of problems there, first you are dropping the last word, you could fix that with: 就像您对问题的评论中指出的那样,您在其中也有很多问题,首先您要删除最后一个单词,可以通过以下方法解决此问题:

    else:
        text_addition += str #If not, then the letter is added to the string text_addition.

    # Avoid dropping last word
    if len(text_addition):
        split_text.append(text_addition)

    while number < len(split_text)-1:
        if split_text[number][0] == letter:

Then I think your IndexError problem comes when you have two "spaces", in that situation you are adding an empty string and since it doesn't have any char [0] is indexError. 然后,我认为您的IndexError问题是在您有两个“空格”时出现的,在这种情况下,您正在添加一个空字符串,并且由于它没有任何char [0],因此是indexError。 You could fix that with: 您可以使用以下方法解决此问题:

    for str in text:
        if str == ' ' or str == "'": # Checks to see whether the letter is a space or apostrophy.
            if text_addition:
                # Here we avoid adding empty strings
                split_text.append(text_addition)
            text_addition = "" #If there is, then the letters collected so far in text_addition are apended to the list split_text and then cleared from text_addition
        else:
            text_addition += str #If not, then the letter is added to the string text_addition.

That it's just to answer your question. 那只是为了回答您的问题。

PD: I little improvement on the last part would be to do: PD:我在最后一部分所做的一点改进是:

    result = []
    for str in split_text:
        if str.startswith(letter):
            result.add(str)
    return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM