简体   繁体   中英

How to check if any of the elements in a list can be found in the given string?

This is the way I'm doing it, but I'm wondering if there's a better way. When I google the problem it suggests using list comprehensions or any() method which either doesn't work for me or I'm misunderstanding them. But I gathered that they just give out boolean values - but I want to keep on using the matched URL if one of the phrases is located in it.

for URL in URLs
    if 'phrase1' in URL or 'phrase2' in URL or 'phrase3' in URL:
        get_subcategories(URL)               #Calling a function wit the matched URL 
        ...

You need a list of phrases to check to iterate over:

phrases = ['phrase1', ...]

if any(phrase in URL for phrase in phrases):
    ...

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