Python 清单A是否有任何不在清单B中的项目

Python 清单A是否有任何不在清单B中的项目,python,list,Python,List,我试图弄清楚,如果我的列表中包含任何不在黑名单中的项目,如何返回true。这听起来可能有点奇怪,但我只想在列表完全由我的黑名单中的项目组成时返回false 这就是我的意思 blacklist = [one, two, three] 以下是我希望发生在以下方面的事情 one two three four = true because four is not in the blacklist one = false because one is in the blacklist one two t

我试图弄清楚,如果我的列表中包含任何不在黑名单中的项目,如何返回true。这听起来可能有点奇怪,但我只想在列表完全由我的黑名单中的项目组成时返回false

这就是我的意思

blacklist = [one, two, three]
以下是我希望发生在以下方面的事情

one two three four = true because four is not in the blacklist
one = false because one is in the blacklist
one two three = false because all are in the blacklist
five = true because five is not in the blacklist

希望这是有意义的。

试试这样:

blacklist = ['one', 'two', 'three'] # these are strings, keep that in mind
lst = [1, 'two', 'three']
new_lst = []
for x in blacklist:
   s = lambda x: True if x not in lst else False
   new_lst.append(s(x))
# Check for overall difference and if it even exists
if any(new_lst):
   print(True)
else:
   print(False)

这将返回您想要的单个结果。列表中有任何不同的项目为True,或所有项目均为False

通过减去两个列表的集合,可以找到这两个列表之间的差异:

这将返回一个列表以查看您的列表和黑名单之间的差异,因此,您可以使用条件查看列表是否为空或不返回false或true

testList = ['one']
blackList = ['one', 'two', 'three']
result = False
for t in testList:
    if (t not in blackList):
        result = True
        break
    else:
        continue

print(result)
像这样的事情?

试试这样的事情:

def not_in_blacklist(*args):

    blacklist = ['one', 'two', 'three']

    return any(arg not in blacklist for arg in args)
结果:

print(not_in_blacklist('one', 'two', 'three', 'four')) -> True
print(not_in_blacklist('one'))                         -> False
print(not_in_blacklist('one', 'two', 'three'))         -> False
print(not_in_blacklist('five'))                        -> True

你说你在试,你试了什么?不是作业。我什么都没试过,我不知道怎么能做这样的事。如果我的列表中没有任何内容不在黑名单中,我希望if语句返回true。转换为集合并检查列表是否是黑名单的子集?简单地说,我没有列出这一点,这是我试图解决的问题。不知道从哪里开始。Google只返回了如何确定一个列表是否包含另一个列表的示例。也许我的措词不好。你知道,如果你真的提供了可执行代码的示例数据,这会有很大的帮助。人们更倾向于实际尝试一些东西,这是隐含的二次型。线性算法是可能的。刚刚意识到。试图修复它。这里有一个提示:黑名单中的x,x
print(not_in_blacklist('one', 'two', 'three', 'four')) -> True
print(not_in_blacklist('one'))                         -> False
print(not_in_blacklist('one', 'two', 'three'))         -> False
print(not_in_blacklist('five'))                        -> True