Tuesday, 15 January 2013

matching list of substrings to a list of strings in Python -


What is a good python method to match a list of substrings in the list of strings, like: < Pre> If sub1 'sub1' in str1 or 'sub2' in str1 or ... str1 in 'subn' or str2 'sub1' or str2 in 'sub2' or ... 'subn' in str2 or \ .. 'subn' in the 'sub1' or strm in strm 'sub2' or ... in the strm 'subn':

In a way, it is to unite them with an understanding of the list, like this:

  strList = [str1, str2, ..., strm] subList = ['sub1', ..., 'subn'] if any (sub in sub for sub sub For str  

very rare Thank you. Div>

You can compile the substrings in a regular expression, and use it to find each string. If you do not have so many substrings that are more than the RE internal limit, then this is probably the most effective Way.

  for re.escape (.soin) (for s) in subList) crexp = re.compile (pattern) if any (crexp for strx Search (s): ...    

No comments:

Post a Comment