gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:06 27 4
gpt4 key购买 nike

我有两个列表:

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)])

关于python - 另一个列表中的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com