how to check if string contains ALL words from the list? - 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: how to check if string contains ALL words from the list? (/thread-28526.html) |
how to check if string contains ALL words from the list? - zarize - Jul-22-2020 Hi guys, How can i check if string contains all words from the list? some_string = 'there is a big apple, but i like banana more than orange' some_list = ['apple', 'banana', 'orange']It should return True RE: how to check if string contains ALL words from the list? - Yoriz - Jul-22-2020 Use the keyword in to check if something is in a sequence.Loop through each item you want to check is in the string. RE: how to check if string contains ALL words from the list? - zarize - Jul-22-2020 well, first of all it should be dynamic, because the string will differ each time. I tried this: some_string = 'there is a big apple, but i like banana more than orange' some_list = ['apple', 'banana', 'orange'] for word in some_list: if word in some_string: print(True)in that case output was: but i need to get return one True or one False
RE: how to check if string contains ALL words from the list? - Yoriz - Jul-22-2020 Flip the logic and check if the word is not in the string return false outside of the loop return True (because it never found an item that was not in the list)
RE: how to check if string contains ALL words from the list? - bowlofred - Jul-22-2020 This is a good place for a comprehension. >>> all([x in some_string for x in some_list]) True RE: how to check if string contains ALL words from the list? - Yoriz - Jul-22-2020 (Jul-22-2020, 05:21 PM)bowlofred Wrote: This is a good place for a comprehension. Unless they are trying to learn how to figure it out themselves and just need a push in the right direction. RE: how to check if string contains ALL words from the list? - zarize - Jul-22-2020 Thank you :) |