Reputation: 143
I´m trying to remove unnecessary words (an, a, the) from a list
Test = ['a', 'an', 'the', 'love']
unWantedWords = ['a', 'an', 'the']
RD1 = [x for x in Test if x != unWantedWords]
print(RD1)
output ->['a', 'an', 'the', 'love']
what is wrong w/ this?
Upvotes: 1
Views: 1547
Reputation: 17606
If you don't mind:
you can simply use 'set' (here is the core algorithm):
>>> Test = ['a', 'an', 'the', 'love']
>>> unWantedWords = ['a', 'an', 'the']
>>> print set(Test) - set(unWantedWords)
set(['love'])
>>> print list(set(Test) - set(unWantedWords))
['love']
This has the advantage of an optimized complexity.
Of course you can wrap this code in order to keep duplicates and order...
Upvotes: 2
Reputation: 2641
This is wrong.
RD1 = [x for x in Test if x != unWantedWords]
your condition of if x != unWantedWords checks if x is equal to the list unWantedWords, instead of checking if x exists or not in unWantedWords.
The condition always is true because x is a string and not a list. Therefore all your words are added to the list.
The correct idiom would be if x not in unWantedWords.
You can do, RD1 = [x for x in Test if x not in set(unWantedWords)]
Upvotes: 1
Reputation: 656
The problem is you are comparing the value x to the entire list unWantedWords.
RD1 = [x for x in Test if x != unWantedWords]
Replace != with not in to check if x is... not in!
RD1 = [x for x in Test if x not in unWantedWords]
Upvotes: 5
Reputation: 838
RD1 = [x for x in Test if x not in unWantedWords]
unWantedWords is an array and you are comapring your words with an array that's why it's not working.
Upvotes: 4