簡體   English   中英

如何檢查是否可以在給定的字符串中找到列表中的任何元素?

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

這是我正在做的方式,但我想知道是否有更好的方法。 當我用谷歌搜索這個問題時,它建議使用列表推導式或 any() 方法,這些方法要么對我不起作用,要么我誤解了它們。 但我發現他們只是給出布爾值 - 但如果其中一個短語位於其中,我想繼續使用匹配的 URL。

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 
        ...

你需要一個短語列表來檢查迭代:

phrases = ['phrase1', ...]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM