Reputation: 39
So here is the sample list:
[work,worked,working,play,works,lotus]
I want to slice the -ed,-ing,-s form of work,the result should be like this:
[work,play,lotus]
So,how can i achieve that with pure python code since the NLTK approach seemed to be inaccurate?
Upvotes: 2
Views: 1675
Reputation: 618
You can simply do this:
List = ['work','worked','working','play']
[item for item in List if not item.endswith(("ed", "ing"))]
Upvotes: 0
Reputation: 150
In python, you can use filter to remove values which ends with ing
or ed
.
your_list = ['work', 'worked', 'working', 'play']
print filter(lambda i: not i.endswith(('ing', 'ed')), your_list)
it returns a list.
['work', 'play']
Upvotes: 1
Reputation: 3580
You can use the below code:
Code:
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
List = ['work','worked','working','play']
List = [stemmer.stem(token) for token in List]
List1=[]
for token in List:
if token not in List1:
List1.append(token)
Output:
['work', 'play']
Upvotes: 1