在 Python 中使用多个 NOT IN 语句

Using multiple NOT IN statements with Python(在 Python 中使用多个 NOT IN 语句)

本文介绍了在 Python 中使用多个 NOT IN 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个循环中取出带有三个特定子字符串的 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 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Python 中使用多个 NOT IN 语句

基础教程推荐