简体   繁体   English

如何检查列表中的每个项目是否出现在另一个列表中的任何项目中?

[英]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.

相关问题 如何检查一个列表中的项目是否可以作为另一个列表中的项目找到 - how to check if items in a list can be found as an item in another list 如果列表中的项目在另一个列表中,我如何签入 Pandas? - How can I check in Pandas if an item from a list is in an another list? 检查另一个列表中是否存在任何两个项目 - Check if any two items are present within another list 如何检查列表中的任何项是否出现在另一个列表中? - How to check if any item in a list occurs in another list? 如何检查列表中的项目是否出现在csv中? - How to check if an item in a list appears in csv? 使用Python Pandas检查项目是否先前出现在项目列表中 - Check whether an item appears previously in a list of items using Python Pandas 如何查找每个字符串是否出现在给定列表中? - How can I find whether every string appears in the given list or not? 如何有效地比较字典中列表中的项目与第二个字典中另一个列表中的第0个项目 - How to efficiently compare the items in a list within a dictionary to the 0th item within another list within a second dictionary 我们如何将列表列表与另一个项目列表附加到每个列表的列表中 - How can we append list of list , with another list of item ,inside list of every list 如何检查具有一系列项目的列表是否存在于另一个列表中 - How do I check if a list with a range of items exists in another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM