-2

Hey guys in new to Python. And I was playing around writing python and I'm stuck.

words = ['w','hello.','my','.name.','(is)','james.','whats','your','name?']

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

'''#position of the invalid words i.e the ones that '''

inpos = -1

for word in words:

    inpos = inpos + 1
    #pass
    for letter in word:
        #print(letter)
        if letter in alphabet:
            pass
            #print('Valid')
        elif letter not in alphabet:
            new_word = word.replace(letter,"")
            print(word)
            print(new_word)
            words[inpos] = new_word

print(words)

This code is meant to clean the text (remove all full stops, commas, and other characters)

The problem is when I run it removes the adds the brackets Heres the output:

Image of output

Can anyone explain why this is happening?

Błotosmętek
  • 12,717
  • 19
  • 29
McLoggy
  • 1
  • 1
  • 1
    'This code is meant to clean the text', is [this](https://stackoverflow.com/questions/4371231/removing-punctuation-from-python-list-items) post an idea? And [this](https://stackabuse.com/removing-stop-words-from-strings-in-python/) one for the stopwords... – R overflow Apr 01 '20 at 12:24

1 Answers1

2

No, it does not add anything. You're printing both old and new word:

        print(word)
        print(new_word)

so when the new_word is (is the word is still (is).

BTW your code has a logical error: when you remove a character you put back new_word in the list, but word is still the old value. So only the last change for every word will be saved in the list words.

Błotosmętek
  • 12,717
  • 19
  • 29