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.
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):
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']
Does Python have a string 'contains' substring method? - Stack Overflow
Checking whether multiple substrings are within a string?
What is the most efficient way to find substrings in strings?
How 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).
Videos
Use the in operator:
if "blah" not in somestring:
continue
Note: This is case-sensitive.
You can use str.find:
s = "This be a string"
if s.find("is") == -1:
print("Not found")
else:
print("Found")
The
find()method should be used only if you need to know the position of sub. To check if sub is a substring or not, use theinoperator. (c) Python reference
For example I am looking for a shorter and cleaner way to do the following:
f = "robot_F.txt"
if "_F_" in f or "_F." in f or "_R_” in f or "_R." in f:
print (" Success")Something like:
if ("_F_", "_F.", "_R_","_R.") in f:
print(" Success")