问题
What is a good pythonic way to match a list of substrings to a list of strings, like the following:
if 'sub1' in str1 or 'sub2' in str1 or ... 'subN' in str1 or\
'sub1' in str2 or 'sub2' in str2 or ... 'subN' in str2 or\
...
'sub1' in strM or 'sub2' in strM or ... 'subN' in strM:
One way is to unite them with list comprehension, like this:
strList = [str1, str2, ..., strM]
subList = ['sub1', ..., 'subN']
if any(sub in str for sub in subList for str in strList):
Is there anything better, like a library function perhaps, to absorb one of the dimensions?
Thank you very much.
回答1:
You could compile the substrings into a regular expression, and use that to search each string. If you don't have so many substrings that the RE exceeds internal limits, this is probably the most efficient way.
pattern = "|".join(re.escape(s) for s in subList)
crexp = re.compile(pattern)
if any(crexp.search(s) for s in strList):
...
回答2:
As described in this answer, a regular expression would be the way to go, since these are modeled as a DFA that can check for all substrings at the same time. You should probably read that answer, as it is quite in-depth.
来源:https://stackoverflow.com/questions/17234965/matching-list-of-substrings-to-a-list-of-strings-in-python