Skip to main content
 首页 » 编程设计

python之另一个列表中的列表项

2025年05月04日10虾米哥

我有两个列表:

list1 = ['home', 'school', 'bus', football'] 
list2 = ['yesterday I went to home', 'I am busy', 385723, 'I feel like  
         playing football', I was tired last week', 'I go to school by  
         bus',' there is a bus stop near my home', 'I am hungry'] 

我想知道如何打印 list2 中包含 list1 中任何项目(至少 1 个)的任何项目? 例如,在我们的示例中,应打印以下内容:

'yesterday I went to home','I feel like playing football', 'I go to school  
 by bus',' there is a bus stop near my home' 

我写了一段代码,但我的应用程序在运行时崩溃了:

    theList = [] 
    i = 0 
    while i < len(list1): 
        for element in list2: 
            if (list1[i]) in element.lower(): 
                theList.append(element) 
        i += 1 
    print(errorList) 

请您参考如下方法:

另一种解决方案:

print([el2 for el2 in list2 if any(el1 in str(el2).split() for el1 in list1)])