[英]Using multiple NOT IN statements with Python
I need to URLs with three specific specific substrings out of a loop.我需要从循环中获取包含三个特定特定子字符串的 URL。 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
Thank you!谢谢!
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
any
returns True if any element of the iterable is true.如果可迭代对象的任何元素为真,则
any
返回 True。 If the iterable is empty, return False.如果可迭代对象为空,则返回 False。 Equivalent to:
相当于:
def any(iterable):
for element in iterable:
if element:
return True
return False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.