Python 另一个列表中列表中的项目
我有两份清单:Python 另一个列表中列表中的项目,python,list,Python,List,我有两份清单: 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
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']
我想知道如何打印列表2中包含列表1中任何项目(至少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)
theList=[]
i=0
而我
试试这个!它来自内存,但应该可以工作:)
如果有错误,请告诉我,试试这个!它来自内存,但应该可以工作:) 如果出现错误,请告诉我,尝试以下方法:
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
试试这个:
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
另一个解决方案:
print([el2 for el2 in list2 if any(el1 in str(el2).split() for el1 in list1)])
另一个解决方案:
print([el2 for el2 in list2 if any(el1 in str(el2).split() for el1 in list1)])
需要拆分成单词并匹配
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']
需要拆分成单词并匹配
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']
这是一个易于阅读的多行解决方案,它不考虑
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)
这是一个易于阅读的多行解决方案,它不考虑
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)
试试这个:-)
清单3的结果是
[“昨天我回家了”,“我想踢足球”,“我去上学了”
“乘公共汽车上学”,“我家附近有一个公共汽车站”]
试试这个:-)
清单3的结果是
[“昨天我回家了”,“我想踢足球”,“我去上学了”
“乘公共汽车上学”,“我家附近有一个公共汽车站”]
如果您直接检查字符串中的'bus',可能会得到错误的结果,因为它会逐个检查sub_字符串,所以如果您在本例中执行此操作:
'bus' in 'I am busy' then it will return True :
因此,解决方案不是检查子字符串,而是逐字检查:
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']
如果您直接检查字符串中的'bus',可能会得到错误的结果,因为它会逐个检查sub_字符串,所以如果您在本例中执行此操作:
'bus' in 'I am busy' then it will return True :
因此,解决方案不是检查子字符串,而是逐字检查:
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']
[a代表列表2中的a(如果有)(str中的s代表列表1中的s)]
“我的应用程序在运行时中断”需要更多详细信息<代码>[a代表列表2中的a(如果有)(str中的s代表列表1中的s)]“我的应用程序在运行时中断”需要更多详细信息。这会引发一个错误TypeError:“int”对象不可编辑
原因是列表中的元素385723
,这会引发一个错误TypeError:“int”对象不可编辑
原因是列表中的元素385723
you@VikasDamodar谢谢,和我的回答一样:)(和split对列表2中的数字385723
不起作用)是的..类似,但只有一行..谢谢你的帮助..我完全忘记了:)我想那应该是“结果”而不是输出。和我的答案一样:)(和split对列表2中的数字385723
不起作用)是的。。相似但只有一行。。谢谢你的帮助。。我完全忘记了:)我认为这应该是“结果”而不是输出。