![](/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.