4
I am analyzing a literary text and am having the following problem to remove a group of words from the text. These are not repeated words. link_words are words that will disturb a later analysis of the text, so I want to remove them. The sets below are merely illustrative!
link_words = ["for","on","this","will","do","did","have","has","my","the","as","be","he"]
words = ["for","on","this","will","do","did","have","has","my","the","as","be","he","virus"]
The idea is to apply map()
and filter()
so that I remove from the list words
the words contained in link_words
. I took a smaller set to make it easier. The exit should be
["virus"]
Apply the filter:
list(filter(lambda x: x in link_words,words))
whose exit is correct:
["for","on","this","will","do","did","have","has","my","the","as","be","he"]
This is what I tried to do after:
be it filtro = filter(lambda x: x in link_words,words)
I’m not getting to the expected result with
list(map(lambda x: words.remove(x),filtro))
What did I miss? How to fix the map?
This answers your question? Remove repeated elements using two lists
– hkotsubo
No need for map and filter, the question suggested above has a solution for your case
– hkotsubo
@hkotsubo I would like to learn how to use them! could help me find the error?
– Laurinda Souza
@hkotsubo In fact it is not repeated words but a group of words that I want to remove from the text to analyze it without them!
– Laurinda Souza
Do not stick to the title of the question, read the answers you have there, because the problem is the same (remove from one list the elements that are in another)
– hkotsubo
However,
map
serves to transform the list elements into something else, then thelambda
should return something. Onlyremove
does not return anything, so the result will be a list full ofNone
. I think map/filter are not the best solution for this case– hkotsubo
@hkotsubo agree but I would like to apply map and filter!
– Laurinda Souza
@hkotsubo The goal is more to learn how to use map and filter, although it may not be the best approach!
– Laurinda Souza