简体   繁体   中英

How to check if every strings from set contains each string from list?

I have got a list of titles in list and set of strings. I want to check whether each of titles from list is in any string in set. The code is working fine if string from list is exactly the same as string in set but I would like to make this working if string in set "contains" string from list. Here is my code:

for link in deduplicated_list:
    rdeep = requests.get(link)
    soup_deep = BeautifulSoup(rdeep.text, 'html.parser')
    if any(ele in str(soup_deep.title) for ele in list_of_books):
        print(soup_deep.title)
list1 = ["cat", "dog", "bird"] 
set2 = ["Book of cats", "Book of elephants", "Book of ants"]

for animal in list1:
    for book in set2:
        if animal in book:
            print(f"FOUND, {animal} in '{book}'")

A Pythonic way to do this would be to chain the for loops .

myset = {"cat meow", "dog ruff", "bird chirp"}

mylist1 = ["cat", "dog"]
mylist2 = ["run", "fun"]

any(animal in animal_sound for animal in mylist1 for animal_sound in myset)
any(animal in animal_sound for animal in mylist2 for animal_sound in myset)

Output:

True False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM