我有两个清单:
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)
答案 0 :(得分:4)
另一种解决方案:
print([el2 for el2 in list2 if any(el1 in str(el2).split() for el1 in list1)])
答案 1 :(得分:3)
试试这个:
for string in list2:
if(type(string)==str): #if the element in list2 is a string
if(set(string.split()) & set(list1)): #split the string into words, and check if it has a set intersection with list1
print(string)
输出:
yesterday I went to home
I feel like playing football
I go to school by bus
there is a bus stop near my home
答案 2 :(得分:0)
# Print Your code here
print('Hello in console window')
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']
theList = []
i = 0
for item in list2:
item1 = str(item).split(" ")
if len(item1) >= 1:
is_in = [x for x in item1 if x.lower() in list1]
else:
is_in = item if item.lower() == list1 else 0
if len(is_in) > 0:
theList.append(item)
for item in theList:
print(item)
让我知道它是否错了,
答案 3 :(得分:0)
需要分成单词并匹配。
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']
theList = []
for item2 in list2:
for item1 in list1:
bfound=False
if type(item2) is str:
words = item2.lower().split()
for word in words:
if word==item1:
theList.append(item2)
bfound=True
break
if bfound:
break
print(theList)
输出
['yesterday I went to home', 'I feel like playing football', 'I go to school by bus', ' there is a bus stop near my home']
答案 4 :(得分:0)
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']
ans = []
for s2 in list2:
l2 = s2.split(' ')
for ss in l2:
if ss in list1:
ans.append(s2)
break
print ans
答案 5 :(得分:0)
这是一个易于阅读的多行解决方案,它没有考虑list2
类型不属于string
的项目:
result = []
for sentence in list2:
if isinstance(sentence, str):
for word in list1:
if word in sentence.split():
result.append(sentence)
break
print(result)
这里也是一个单行风格的解决方案(我看到它已经由@grimek提供),它将list2
中的任何项目转换为string
类型:
result = [sentence for sentence in list2 if any(word in str(sentence).split() for word in list1)]
print(result)
答案 6 :(得分:0)
list3 = [s for s in list2 if (type(s)==str) and (set(s.split())&set(list1))]
试试这个: - )
list3的结果是
<'>''昨天我回家','我觉得自己喜欢踢足球','我去了 乘公共汽车上学','我家附近有一个公共汽车站']
答案 7 :(得分:0)
如果您直接检查是否有“公共汽车”,那就有一个问题。在string中,你可能会得到错误的结果,因为它会检查sub_string by_sub_string,所以如果你在这个例子中这样做:
'bus' in 'I am busy' then it will return True :
所以解决方案不是检查sub_string,而是逐字检查:
print([j for i in list1 for j in list2 if isinstance(j,str) if i in j.split()])
输出:
['yesterday I went to home', ' there is a bus stop near my home', 'I go to school by bus', 'I go to school by bus', ' there is a bus stop near my home', 'I feel like playing football']