matching list of substrings to a list of strings in Python

久未见 提交于 2019-12-08 05:11:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!