pythonarraysliststem

How to store stemmed strings into single array?


I am new to python and am just trying to write a simple program that would stem the words in a string and output an array/list that contains the stemmed words but I can seem to get them into a single array. This is the code i have and I included the output. Thanks in advance for any help!

from nltk.stem import PorterStemmer
from nltk.tokenize import sent_tokenize, word_tokenize

ps = PorterStemmer()

new_text = "My two friends are getting married tomorrow and I could not be 
more excited for them"


words = word_tokenize(new_text)

for w in words:
    stems = [ps.stem(w)]
    print(stems)

My output:

['My']
['two']
['friend']
['are']
['get']
['marri']
['tomorrow']
['and']
['I']
['could']
['not']
['be']
['more']
['excit']
['for']
['them']

Solution

  • You can use a list comprehension instead.

    print([ps.stem(w) for w in words])