我需要从一个循环中取出带有三个特定子字符串的 URL.以下代码有效,但我确信有一种更优雅的方法:
I need to URLs with three specific specific substrings out of a loop. The following code worked, but I am sure there's a more elegant way to do it:
for node in soup.findAll('loc'):
url = node.text.encode("utf-8")
if "/store/" not in url and "/cell-phones/" not in url and "/accessories/" not in url:
objlist.loc.append(url)
else:
continue
谢谢!
url = node.text.encode("utf-8")
sub_strings = ['/store','/cell-phones/','accessories']
if not any(x in url for x in sub_strings):
objlist.loc.append(url)
else:
continue
来自 docs:
any
返回 True.如果可迭代对象为空,则返回 False.相当于:
any
returns True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:
def any(iterable):
for element in iterable:
if element:
return True
return False
这篇关于在 Python 中使用多个 NOT IN 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!