根据从更大的列表列表中匹配的字符串提取子列表项

Extract sub-list items based on matching strings from a larger list of lists

我有以下搜索列表,可用于根据更大的列表列表搜索其中的任何项目。我希望结果是完整的子列表,但我似乎只得到了项目本身。

search_list = ['a', 'b', 'x']
list_of_lists = [['axh', 'opp'], ['l n', '3b v'], ['09,8', 'cdj l', 'sd9 c']
new_lst=[]
for z in list_of_lists:
    yll = [x for x in z if any(w in x for w in search_list)]
    n_lst.append(yll)

new_lst 的输出:

new_lst = [['a xh'], ['3 b v'], []]

我在获得此输出之后显示结果列表中与 search_list

中的任何项目相匹配的所有项目
[['a xh', 'opp'], ['l n', '3b v'], []]

如有任何建议或提示,我们将不胜感激。

谢谢

search_list = ['a', 'b', 'x']
list_of_lists = [['axh', 'opp'], ['l n', '3b v'], ['09,8', 'cdj l', 'sd9 c']]
new_lst=[]
for sublist in list_of_lists:
    for element in sublist:
        for item in search_list:
            if item in element and sublist not in new_lst:
                new_lst.append(sublist)

print(new_lst)

输出:[['axh', 'opp'], ['l n', '3b v']]

append 如果 search_list 的任何字符匹配,则为 new_list 的子列表。

search_list = ['a', 'b', 'x']
list_of_lists = [['axh', 'opp'], ['l n', '3b v'], ['09,8', 'cdj l', 'sd9 c']]

new_list = []
for sub in list_of_lists:
    for l in sub:
        if any(w in l for w in search_list):
            new_list.append(sub)
print(new_list)

# Output
# [['axh', 'opp'], ['l n', '3b v']]

如果您不介意将子列表转换为元组,则可以使用以下集合理解:

result = {tuple(sublist) for sublist in list_of_lists for element in sublist for item in search_list
      if item in element}

#output: {('axh', 'opp'), ('l n', '3b v')}