Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a nested list by stemming the words inside the nested lists?

I’ve a Python list with several sub lists having tokens as tokens.
I want to stem the tokens in it so that the output will be as stemmed_expected.

tokens = [['cooked', 'lovely','baked'],['hotel', 'going','liked'],['room','looking']]

stemmed_expected: [['cook', 'love','bake'],['hotel', 'go','like'],['room','look']]

The for loop I tried is follows:

from nltk.stem import PorterStemmer  
ps = PorterStemmer()

stemmed_actual = []

for m in tokens:
    for word in m:
        word = ps.stem(word)
        stemmed_actual.append(word)

But the output of this for loop is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

stemmed_actual = ['cook', 'love', 'bake', 'hotel', 'go', 'like', 'room', 'look']

How can I modify the for loop to get the stemmed words in sub lists as it is in stemmed_expected?

>Solution :

You can use nested list comprehension:

from nltk.stem import PorterStemmer

tokens = [['cooked', 'lovely','baked'],['hotel', 'going','liked'],['room','looking']]

ps = PorterStemmer()
stemmed = [[ps.stem(word) for word in sublst] for sublst in tokens]

print(stemmed)
# [['cook', 'love', 'bake'], ['hotel', 'go', 'like'], ['room', 'look']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading