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 Overflowpython - How to check if a string is a substring of items in a list of strings - Stack Overflow
How to check if any element in a list contains a substring
How to detect if a string contains a substring of a list (or dictionary)
python - Check if substring is in a list of strings? - Stack Overflow
To check for the presence of 'abc' in any string in the list:
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in xs):
...
To get all the items containing 'abc':
matching = [s for s in xs if "abc" in s]
Just throwing this out there: if you happen to need to match against more than one string, for example abc and def, you can combine two comprehensions as follows:
matchers = ['abc','def']
matching = [s for s in my_list if any(xs in s for xs in matchers)]
Output:
['abc-123', 'def-456', 'abc-456']
Say I have a list of strings like: ["Hey dude", "Hey bro", "Sup dude", "Hey bud"]
How do I check if *any* of those strings in the list contain the word "Sup"?Alternatively, in my case, I could also check if *all* of the strings contain "Hey" , which I also don't know how to do.
(Sorry, I'm new to Python)
Edit: Thank you so much for all the help, guys <3
Posted code
The OP's posted code using any() is correct and should work. The spelling of "worldlist" needs to be fixed though.
Alternate approach with str.join()
That said, there is a simple and fast solution to be had by using the substring search on a single combined string:
>>> wordlist = ['yellow','orange','red']
>>> combined = '\t'.join(wordlist)
>>> 'or' in combined
True
>>> 'der' in combined
False
For short wordlists, this is several times faster than the approach using any.
And if the combined string can be precomputed before the search, the in-operator search will always beat the any approach even for large wordlists.
Alternate approach with sets
The O(n) search speed can be reduced to O(1) if a substring set is precomputed in advance and if we don't mind using more memory.
Precomputed step:
from itertools import combinations
def substrings(word):
for i, j in combinations(range(len(word) + 1), 2):
yield word[i : j]
wordlist = ['yellow','orange','red']
word_set = set().union(*map(substrings, wordlist))
Fast O(1) search step:
>>> 'or' in word_set
True
>>> 'der' in word_set
False
You can import any from __builtin__ in case it was replaced by some other any:
>>> from __builtin__ import any as b_any
>>> lst = ['yellow', 'orange', 'red']
>>> word = "or"
>>> b_any(word in x for x in lst)
True
Note that in Python 3 __builtin__ has been renamed to builtins.
Use a generator together with any, which short-circuits on the first True:
if any(ext in url_string for ext in extensionsToCheck):
print(url_string)
EDIT: I see this answer has been accepted by OP. Though my solution may be "good enough" solution to his particular problem, and is a good general way to check if any strings in a list are found in another string, keep in mind that this is all that this solution does. It does not care WHERE the string is found e.g. in the ending of the string. If this is important, as is often the case with urls, you should look to the answer of @Wladimir Palant, or you risk getting false positives.
extensionsToCheck = ('.pdf', '.doc', '.xls')
'test.doc'.endswith(extensionsToCheck) # returns True
'test.jpg'.endswith(extensionsToCheck) # returns False
I'd compile the list into a fnmatch pattern:
import fnmatch
pattern = '*'.join(contains)
filetered_filenames = fnmatch.filter(master_list, pattern)
This basically concatenates all strings in contains into a glob pattern with * wildcards in between. This assumes the order of contains is significant. Given that you are looking for prefixes, suffixes and (parts of) dates in between, that's not that much of a stretch.
It is important to note that if you run this on an OS that has a case-insensitive filesystem, that fnmatch matching is also case-insensitive. This is usually exactly what you'd want in that case.
You're looking for something like that (using list comprehension and all():
>>> files = ["prefix_20160817_suffix", "some_other_file_with_suffix"]
>>> contains = ['prefix', '2016', 'suffix']
>>> [ f for f in files if all(c in f for c in contains) ]
['prefix_20160817_suffix']