filtering a string - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: filtering a string (/thread-6558.html) |
filtering a string - hello_its_me - Nov-28-2017 Hi everyone, I am looking for a way to filter words out of a string. I need it to work like this: The string contains known words and unknown words and I need to divide both. For example: input(word something) or input(word word something word) word should go into a variable and something into another Any ideas? Thanks RE: filtering a string - sparkz_alot - Nov-28-2017 If it only a few words, put them each in their own list, then test if the word exists in one or the other list. good_words = ['good', 'better', 'best', 'great'] bad_words = ['bad', 'worst', 'terrible', 'evil'] reply = input("Enter a word: ") if reply in good_words: a_reply = reply elif reply in bad_words: a_reply = reply else: a_reply = "Not in my vocabulary" RE: filtering a string - hello_its_me - Nov-28-2017 That is not that what I am looking for there are more words that need to be filtered like: hello my name is known words are: hello, name unknown words are: my, is I need both in a variable because I need to decide with them witch "if" to use but still need the words in another variable that are not known RE: filtering a string - sparkz_alot - Nov-28-2017 You could look at NLTK. Along with my original example, that gives you both ends of the spectrum. Other than that, you need to be more specific as to what you are looking to do. RE: filtering a string - ODIS - Nov-28-2017 @hello_its_me, something like this? vocabulary = ["hello", "name"] words = input("Enter words: ") known_words = [] unknown_words = [] for word in words.split(" "): if word in vocabulary: if word not in known_words: known_words.append(word) elif word not in unknown_words: unknown_words.append(word) print("Known words: " + str(known_words)) print("Unknown words: " + str(unknown_words)) RE: filtering a string - nilamo - Nov-28-2017 That is what you're looking for, though. Loop through the sentence, word-by-word, and build up a result list of what your expected output is. RE: filtering a string - hello_its_me - Nov-29-2017 I will try them out later and let you know but thanks for the help |