Home > database >  IndexError in Python Function when iterating thru a split string with Range and Len methods
IndexError in Python Function when iterating thru a split string with Range and Len methods

Time:07-23

Problem Statement: I am attempting to write a function that accepts a string and list.

  • A string of words which I will use the .split() method to turning into a list
  • make all words in list lowercase to make checking uniform
  • iterate thru the list checking to see if the string list has words that are in the array.
  • If they are it will remove the word
  • join the list of non-filler words back into a string

Traceback:

Traceback (most recent call last):
  File "main.py", line 55, in <module>
    no_filler = remove_filler_words(no_punction,filler_words);
  File "main.py", line 37, in remove_filler_words
    word_list[cap_word] = word_list[cap_word].lower()
IndexError: list index out of range

Code:

def remove_filler_words(cleaned_string:string, filler_words:list):
    cleaned_sentence = ""
    word_list = cleaned_string.split()
    for cap_word in range(len(word_list)):
        word_list[cap_word] = word_list[cap_word].lower()
        for filler_word in filler_words:
            if filler_word in word_list:
                word_list.remove(filler_word)
                line_join = " ".join(word_list)
                cleaned_sentence  = line_join
   
 return cleaned_sentence

Replit: https://replit.com/join/dpxpwamhgy-terry-brooksjr

CodePudding user response:

Try to create the duplicate of word_list, e.g. copy_word_list and delete words from this variable.

code will be like this:

def remove_filler_words(cleaned_string:string, filler_words:list):
    cleaned_sentence = ""
    word_list = cleaned_string.split()
    #create duplicate
    copy_word_list = word_list
    for cap_word in range(len(word_list)):
        word_list[cap_word] = word_list[cap_word].lower()
        for filler_word in filler_words:
            if filler_word in word_list:
                #remove from copy
                copy_word_list.remove(filler_word)
                line_join = " ".join(word_list)
                cleaned_sentence  = line_join
   
 return cleaned_sentence

Hope it helps)

CodePudding user response:

Here's the solution I came up with:

def remove_filler_words(cleaned_string, filler_words):
    word_list = cleaned_string.split()
    new_word_list = []
    for cap_word in range(len(word_list)):
        lower_word = word_list[cap_word].lower()
        new_word_list.append(lower_word)
        for filler_word in filler_words:
            if filler_word in new_word_list:
                new_word_list.remove(filler_word)
                cleaned_sentence = " ".join(new_word_list)

    return cleaned_sentence

I added all of the lowercase words, saved to lower_word, to a new list to account for the index error. Then, instead of having an empty list declared at the beginning, I assigned the cleaned_sentence variable to the join function which produces the expected result when returned.

  • Related