[英]Matching a substring from a substring list in a list of strings
I have a list of substrings which has about 10000 entries -我有一个包含大约 10000 个条目的子字符串列表 -
substr_ls = ['N_COULT16_1 1', 'S_COULT2', 'XBG_F 1', 'FAIRWY_3', .....]
I have a list of strings which has about 100 entries -我有一个包含大约 100 个条目的字符串列表 -
main_str_ls = ['N_COULT16_1 1XF', 'S_COULT2_RT', 'XBG_F TX300 1', 'FAIRWY_34_AG', ....]
As you see, the substrings are not perfect substrings of strings from main_str_ls
.如您所见,子字符串不是来自
main_str_ls
的字符串的完美子字符串。 The sequence of alphabets, numbers, etc from substring will have to match the sequence from string for it to be a match. substring 中的字母、数字等序列必须与字符串中的序列匹配才能匹配。 For example -
'XBG_F 1'
is a match with 'XBG_F TX300 1'
because the sequence is a match even though there is a 'TX300'
in the middle of 'XBG_F'
and '1'
What I'm currently doing is using this function -例如 -
'XBG_F 1'
与'XBG_F TX300 1'
匹配,因为即使在'XBG_F'
和'1'
中间有一个'TX300'
,序列也是匹配的 我目前正在做的是使用这个function -
def is_subsequence(pattern, items_to_use):
items_to_use = (x for x in items_to_use)
return all(any(x == y for y in items_to_use) for x, _ in itertools.groupby(pattern))
from Finding a substring in a jumbled string by iterating over main_str_ls
(contents of main_str_ls
used as items_to_use
) and substr_ls
(contents of substr_ls
used as pattern
) and when I find a match, it breaks the loop and does some stuff.通过迭代
main_str_ls
( main_str_ls
的内容用作items_to_use
)和substr_ls
( substr_ls
的内容用作pattern
) 在混乱的字符串中查找 substring ,当我找到匹配项时,它会中断循环并执行一些操作。 Something like this -像这样的东西-
for main_str in main_str_ls:
main_str = main_str.strip()
for substr in substr_ls:
substr = substr.strip()
if is_subsequence(substr, main_str):
**do stuff**
Is there a better way or a pythonic approach for doing this?有没有更好的方法或pythonic方法来做到这一点?
One of the diffence between what you need vs the jumbled string question is they are concerned about allowing repeats.您需要的内容与混乱的字符串问题之间的区别之一是他们担心允许重复。 I don't think you can use that design directly.
我认为您不能直接使用该设计。 Instead, try this link https://www.geeksforgeeks.org/given-two-strings-find-first-string-subsequence-second/
相反,请尝试此链接https://www.geeksforgeeks.org/given-two-strings-find-first-string-subsequence-second/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.