-3

I need to compare a string or list to another list of unwanted words to leave only the key words behind for example:

>filter_words(['how', 'about', 'i', 'go', 'through', 'that', 'little', 'passage', 'to', 'the', 'south'], skip_words)
>> Expected outcome ['go', 'passage', 'south']

This is needs to be compared to skip_words which is:

>skip_words = ['a', 'about', 'all', 'an', 'another', 'any', 'around', 'at',
              'bad', 'beautiful', 'been', 'better', 'big', 'can', 'every', 'for',
              'from', 'good', 'have', 'her', 'here', 'hers', 'his', 'how',
              'i', 'if', 'in', 'into', 'is', 'it', 'its', 'large', 'later',
              'like', 'little', 'main', 'me', 'mine', 'more', 'my', 'now',
              'of', 'off', 'oh', 'on', 'please', 'small', 'some', 'soon',
              'that', 'the', 'then', 'this', 'those', 'through', 'till', 'to',
              'towards', 'until', 'us', 'want', 'we', 'what', 'when', 'why',
              'wish', 'with', 'would']

This example uses a list of words rather than a basic string so this is more a preliminary function rather than the end result but I haven't been able to find a solution as of yet.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Red
  • 17
  • 6
  • So what have you tried, and what precisely is the problem with it? SO is neither a code-writing nor tutorial service. – jonrsharpe Oct 14 '15 at 15:46

1 Answers1

0

You should read the python documentation for list comprehension, this snippet should help:

newlist = []

for i in filter_words:
    if i not in skip_words:
        newlist.append(i)
iri
  • 735
  • 4
  • 8
  • Nope: http://meta.stackoverflow.com/q/250177/3001761 – jonrsharpe Oct 14 '15 at 15:49
  • All well and good linking to a meta post, but I think that in this collaborative environment it is courteous to leave a comment explaining the downvote. By all means make your downvote, but I think it helps everyone (the questioner and the answerer) if you explain why. – iri Oct 14 '15 at 15:51
  • 1
    You're entitled to your opinion, but this has been discussed multiple times on Meta (that's by no means the only discussion, see e.g. the linked questions) and repeatedly dismissed for various reasons. Downvotes are anonymous, no explanation required, and **that's not going to change**. So don't waste time asking; if people want to comment they can already, and if they don't want to they don't have to. – jonrsharpe Oct 14 '15 at 15:52
  • Well I am fairly new here, so the fact I got a downvote is fairly puzzling, I guess it is because SO isnt a code writing service so people took objection to me giving an answer. Or maybe my answer was not well explained, or maybe it was wrong. I will never know :). – iri Oct 14 '15 at 15:54
  • My guess: Posting a code-only (first revision) answer to a broad duplicate question. – vaultah Oct 14 '15 at 15:55
  • Thanks for feedback, so code-only answers are frowned upon? That is not really covered in the welcome tour. Good to know for next time. – iri Oct 14 '15 at 15:56
  • 1
    Thanks for that, I honestly just needed to know the right way of writing it. I've only been doing python for a week so I'm getting used to using the documentation relative to the problem I'm trying to solve. – Red Oct 14 '15 at 16:10
  • 1
    It's possible this: http://meta.stackoverflow.com/questions/307197/should-we-avoid-answering-questions-with-a-negative-score – Sobrique Oct 15 '15 at 09:04