Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字符串列表中的子字符串列表匹配子字符串_Python_String_List_Substring - Fatal编程技术网

Python 从字符串列表中的子字符串列表匹配子字符串

Python 从字符串列表中的子字符串列表匹配子字符串,python,string,list,substring,Python,String,List,Substring,我有一个包含10000个条目的子字符串列表- substr_ls = ['N_COULT16_1 1', 'S_COULT2', 'XBG_F 1', 'FAIRWY_3', .....] main_str_ls = ['N_COULT16_1 1XF', 'S_COULT2_RT', 'XBG_F TX300 1', 'FAIRWY_34_AG', ....] 我有一个字符串列表,大约有100个条目- substr_ls = ['N_COULT16_1 1', 'S_COULT2', 'X

我有一个包含10000个条目的子字符串列表-

substr_ls = ['N_COULT16_1 1', 'S_COULT2', 'XBG_F 1', 'FAIRWY_3', .....]
main_str_ls = ['N_COULT16_1 1XF', 'S_COULT2_RT', 'XBG_F TX300 1', 'FAIRWY_34_AG', ....]
我有一个字符串列表,大约有100个条目-

substr_ls = ['N_COULT16_1 1', 'S_COULT2', 'XBG_F 1', 'FAIRWY_3', .....]
main_str_ls = ['N_COULT16_1 1XF', 'S_COULT2_RT', 'XBG_F TX300 1', 'FAIRWY_34_AG', ....]
如您所见,子字符串不是
main\u str\u ls
中字符串的完美子字符串。子字符串中的字母、数字等序列必须与字符串中的序列匹配,才能使其匹配。例如-<代码> xbgff 1′<代码>是与<代码> xbgff tx300 1′<代码>的匹配,因为序列是匹配的,即使在<代码>“xbgff′/代码>和<代码> 1'′/代码>的中间有一个<代码> 'tx> <代码> >我现在正在使用的是这个函数-< /p>
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))
通过迭代
main_str_ls
(将
main_str_ls
的内容用作
项,以使用
)和
substr_ls
(将
substr_ls
的内容用作
模式),当我找到匹配项时,它会打破循环并执行一些操作。像这样的-

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**

有没有更好的方法或类似python的方法来实现这一点?

您所需要的与混乱字符串问题之间的区别之一是,他们担心允许重复。我认为你不能直接使用那种设计。
相反,请尝试此链接

我会将
子字符串
列表更改为正则表达式的
子字符串
列表<代码>“XBG\u F 1”
可以变成
r“XBG\u F.*1”
,然后使用
如果重新匹配(重新匹配,测试匹配):…
最好的方法仍然是在两个列表上迭代,对吗?是的,复杂性是O(MN(L1+L2))。考虑到你的问题规模,我认为这是可行的