Try this test:
any(substring in string for substring in substring_list)
It will return True if any of the substrings in substring_list is contained in string.
Note that there is a Python analogue of Marc Gravell's answer in the linked question:
from itertools import imap
any(imap(string.__contains__, substring_list))
In Python 3, you can use map directly instead:
any(map(string.__contains__, substring_list))
Probably the above version using a generator expression is more clear though.
Answer from Sven Marnach on Stack OverflowHow do I check if string contains all substrings in list?
You could try
if all([val in string for val in list]):
Which checks the truth of each check and returns True only if all are True. Otherwise you're looking at a loop (or if your lists get really big a generator).
How to detect if a string contains a substring of a list (or dictionary)
(Python) How to check if an element in a string is a part of a Tuple/List?
I don't know what you mean by "an element in a string," as a string doesn't consist of smaller elements. Do you mean how to check whether a particular value is an element in a list/tuple?
-
https://www.google.com/search?q=python%20list%20include
-
http://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exist-in-a-list
How to check if any element in a list contains a substring
I have a list, that takes inputs from the user, this means it can change size; for example: list = ["test", "do"] or list = ["a", "b", "c"]
I want to check if variable string, contains all the substrings in list.
I thought if I used:
if list in string: that would work, but it didn't, TypeError: 'in <string>' requires string as left operand, not list
Is there another function/method for this? Or am I going to have to loop len(list) times (I really don't want to do that, because I check hundreds of changing strings)?
You could try
if all([val in string for val in list]):
Which checks the truth of each check and returns True only if all are True. Otherwise you're looking at a loop (or if your lists get really big a generator).
I don't really understand your question, but the easiest to understand solution is probably
if len(set(sub) - set(super)) > 0:
Another one, straightforward, but more complex, is
if all(el in sub for el in super):