简体   繁体   English

在 Python 中使用多个 NOT IN 语句

[英]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

From the docs :文档

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM