Search code examples
pythonnlpnltk

for loop for nltk stopwords goes to infinity


import nltk as nt

txt="The latest version of Anaconda comes with Python 3.8. But sometimes you need to use an earlier release. With Anaconda, the preferred way to use a previous version of Python is to create a separate conda environment for each project."

word_tok=nt.word_tokenize(txt)
stopWords=nt.corpus.stopwords.words("english")
print(word_tok)

wordClear=[]
for i in word_tok: # infinity loop
      if i not in stopWords:
        word_tok.append(i)

what is the problem?

I see always up stated form but my project's this form is infinity loop but

for i in word_tok:
  print(i)

This for did'nt infinity loop.


Solution

  • import nltk as nt
    
    txt="The latest version of Anaconda comes with Python 3.8. But sometimes you need to use an earlier release. With Anaconda, the preferred way to use a previous version of Python is to create a separate conda environment for each project."
    
    word_tok=nt.word_tokenize(txt)
    stopWords=nt.corpus.stopwords.words("english")
    print(word_tok)
    
    wordClear=[]
    for i in word_tok: # infinity loop
          if i not in stopWords:
            # Do you wantthis?
            wordClear.append(i)
    

    You are updating the array word_tok and dynamically add new i's. So it might keep on increasing and never end.