[英]How Can I Check if Every Item in a List Appears Within Any Items in Another List?
For example: If I have 2 lists,例如:如果我有 2 个列表,
list1 = ["apple","banana","pear"]
list2 = ["Tasty apple treat", "Amazing banana snack", "Best pear soup"]
I would like to check if every string in list1 appears in any item in list2.我想检查 list1 中的每个字符串是否出现在list2 中的任何项目中。 So in the example, it would get True in return.
所以在这个例子中,它会得到 True 作为回报。 But if list2 looked like this...
但如果 list2 看起来像这样......
list2 = ["Tasty apple treat", "Best pear soup", "Delicious grape pie"]
...it would return false since "banana" doesn't appear in any items in the list. ...它会返回 false,因为“香蕉”没有出现在列表中的任何项目中。
I have tried by making a tfList that would hold True and False values and then I could just check if any items in the tfList were false.我尝试制作一个包含 True 和 False 值的 tfList,然后我可以检查 tfList 中的任何项目是否为假。
tfList = []
for x in list1:
if (x in list2):
tfList.append(True)
else:
tfList.append(False)
I also tried this but it may have been a worse attempt:我也试过这个,但它可能是一个更糟糕的尝试:
if all(True if (x in list2) else False for x in list1):
The first one returned all False values and the second one didn't run the if statement as true and instead ran the else even though I used testing lists like the first examples.第一个返回所有 False 值,第二个没有将 if 语句作为 true 运行,而是运行 else,即使我像第一个示例一样使用了测试列表。
**I'm very new to this so apologies if my attempts seem insane. **如果我的尝试看起来很疯狂,我对此很抱歉。
You want to check if each string of list1
is a sub-string of at least one element of list2
.您想检查
list1
的每个字符串是否是list2
的至少一个元素的子字符串。
The reason your first approach returns always False
is because you are not checking if x
appears in each element of list2
, rather if x
is an element of list2
.您的第一种方法总是返回
False
的原因是因为您没有检查x
是否出现在list2
的每个元素中,而是检查x
是否是list2
的元素。
You could accomplish your goal by:您可以通过以下方式实现您的目标:
def appears_in(list1, list2):
for word in list1:
appears = False
for sentence in list2:
if word in sentence:
appears = True
break
if not appears:
return False
return True
This should work:这应该有效:
find = True
for word in list1:
auxFind = False
for phrase in list2:
if(word in phrase):
auxFind = True
if(not auxFind):
find = False
print(find)
What it does is verify if every word in list1 appears at least one time on list2 and return True if does find.它的作用是验证 list1 中的每个单词是否在 list2 上至少出现一次,如果找到则返回 True。
all(
any(
word1 in map(str.lower, word2.split())
for word2 in list2
)
for word1 in list1
)
Depending on how nice your inputs are, you can replace map(str.lower, word2.split())
with word2
.根据您的输入有多好,您可以将
map(str.lower, word2.split())
替换为word2
。
Maybe this helps也许这有帮助
list1 = ["apple","banana","pear"]
list2 = ["Tasty apple treat", "Amazing banana snack", "Best pear soup"]
#return true/false for presense of list1 in any of items in list2
tf_list = [i in x for i in list1 for x in list2]
# if all items in list1 in list2
tf_list_all = all([i in x for i in list1 for x in list2])
# if any of items of list1 is in list 2
tf_list_any = any([i in x for i in list1 for x in list2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.