How do you check if a list of strings is in a string python?
If you want exact matches of words then consider word tokenizing the target string. I use the recommended word_tokenize from nltk: Show
Here is the tokenized string from the accepted answer:
The accepted answer gets modified as follows:
As in the accepted answer, the word "more" is still matched. If "mo" becomes a match string, however, the accepted answer still finds a match. That is a behavior I did not want.
Using word tokenization, "mo" is no longer matched:
That is the additional behavior that I wanted. This answer also responds to the duplicate question here. We are given a String and our task is to test if the string contains elements from the list. Example: Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True Naive Approach checking each word in the stringHere we are splitting the string into list of words and then matching each word of this list with the already present list of words we want to check. Python3
Output: The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element Using list comprehension to check if string contains element from listThis problem can be solved using the list comprehension, in this, we check for the list and also with string elements if we can find a match, and return true, if we find one and false is not using the conditional statements. Python3
Output: The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True Using any() to check if string contains element from listUsing any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list. Python3
Output: The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True Using find() method to check if string contains element from listHere we are using the find() method to check the occurrence of the word and it returns -1 if the word does not exist in the list. Python3
Output: The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True How do you check if a list of strings contains a string in Python?Use any() function to check if a list contains a substring in Python. The any(iterable) with iterable as a for-loop that checks if any element in the list contains the substring and returns the Boolean value.
How do you check if a list of string contains a string?Using String.
contains() method for each substring. You can terminate the loop on the first match of the substring, or create a utility function that returns true if the specified string contains any of the substrings from the specified list.
How do you check if an item in a list is in a string Python?Use the any() function to check if a string contains an element from a list, e.g. if any(substring in my_str for substring in my_list): . The any() function will return True if the string contains at least one element from the list and False otherwise.
How do you check if a list of characters is in a string Python?Using Python's "in" operator
The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .
|