![](/img/trans.png)
[英]Removing elements of a list if they can be found in some given string in Python
[英]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.