Python 查找不在列表中的元素

Python 查找不在列表中的元素,python,list,Python,List,这是我的密码: item = [0,1,2,3,4,5,6,7,8,9] z = [] # list of integers for item in z: if item not in z: print item z包含整数列表。我想将项目与z进行比较,并打印出与项目进行比较时不在z中的数字 当比较时,我可以打印z中的元素,而不是item,但当我尝试使用上面的代码执行相反操作时,则不会打印任何内容 有什么帮助吗?如果您运行一个循环,从z获取项目,您如何期望它们不在z

这是我的密码:

item = [0,1,2,3,4,5,6,7,8,9]
z = []  # list of integers

for item in z:
    if item not in z:
        print item
z
包含整数列表。我想将
项目
z
进行比较,并打印出与
项目
进行比较时不在
z
中的数字

当比较时,我可以打印
z
中的元素,而不是
item
,但当我尝试使用上面的代码执行相反操作时,则不会打印任何内容


有什么帮助吗?

如果您运行一个循环,从z获取项目,您如何期望它们不在z中?我认为将不同列表中的项目与z进行比较更有意义。

您的代码没有达到我认为您认为的效果。z:中项目的
行将迭代通过
z
,每次使
项目
等于
z
的一个元素。因此,原始的
列表在您对其执行任何操作之前会被覆盖

list1 = [1,2,3,4]; list2 = [0,3,3,6]

print set(list2) - set(list1)
我想你想要这样的东西:

item = [0,1,2,3,4,5,6,7,8,9]

for element in item:
    if element not in z:
        print(element)
但你可以很容易地这样做:

[x for x in item if x not in z]
或者(如果您不介意丢失非唯一元素的副本):


不,z是未定义的。项包含整数列表

我想你要做的是:

#z defined elsewhere
item = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for i in item:
  if i not in z: print i

正如其他答案中所述,您可能希望尝试使用集合。

您的代码是不可操作的。根据循环的定义,“item”必须在Z中。a“For…”in“循环在Python中的意思是“通过名为“Z”的列表循环,每次循环时,给我列表中的下一项,并将其称为“item”


我认为您的困惑是因为您两次使用变量名“item”,这意味着两个不同的东西。

在遍历z时,您将item重新分配给z中的值。所以在for循环中的第一次,item=0,next item=1,等等。。。你从来没有检查过一张清单和另一张清单

要非常明确地做到这一点:

>>> item = [0,1,2,3,4,5,6,7,8,9]
>>> z = [0,1,2,3,4,5,6,7]
>>> 
>>> for elem in item:
...   if elem not in z:
...     print elem
... 
8
9

使用列表理解:

print [x for x in item if x not in Z]
或使用过滤功能:

filter(lambda x: x not in Z, item)
如果正在检查的列表包含非唯一元素,则以任何形式使用
set
可能会产生错误,例如:

print item

Out[39]: [0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print Z

Out[40]: [3, 4, 5, 6]

set(item) - set(Z)

Out[41]: {0, 1, 2, 7, 8, 9}
vs列表理解如上所述

print [x for x in item if x not in Z]

Out[38]: [0, 1, 1, 2, 7, 8, 9]
或过滤功能:

filter(lambda x: x not in Z, item)

Out[38]: [0, 1, 1, 2, 7, 8, 9]

item
z
是排序迭代器的情况下,我们可以通过这样做将复杂性从
O(n^2)
降低到
O(n+m)

def iexclude(sorted_iterator, exclude_sorted_iterator):
    next_val = next(exclude_sorted_iterator)
    for item in sorted_iterator:
        try:
            while next_val < item:
                next_val = next(exclude_sorted_iterator)
                continue
            if item == next_val:
                continue
        except StopIteration:
            pass
        yield item
def iexclude(排序迭代器,排除排序迭代器):
下一个值=下一个(排除排序迭代器)
对于排序迭代器中的项:
尝试:
下一个值<项目:
下一个值=下一个(排除排序迭代器)
持续
如果项目==下一个值:
持续
除停止迭代外:
通过
收益项目

如果这两个是迭代器,我们也有机会减少内存占用,不将
z
exclude\u sorted\u iterator
)存储为列表。

如果选中的列表包含非唯一元素,则使用
set
将无法正常工作,as
set
将首先从列表中删除非唯一元素的所有实例,但只有一个除外。for循环(这是批准的答案)big Oh是
O(n)
并且您的答案在for循环中嵌套了一段时间,因此在您的情况下复杂性将增加,即
O(n^2)
这是否回答了您的问题?
print [x for x in item if x not in Z]

Out[38]: [0, 1, 1, 2, 7, 8, 9]
filter(lambda x: x not in Z, item)

Out[38]: [0, 1, 1, 2, 7, 8, 9]
def iexclude(sorted_iterator, exclude_sorted_iterator):
    next_val = next(exclude_sorted_iterator)
    for item in sorted_iterator:
        try:
            while next_val < item:
                next_val = next(exclude_sorted_iterator)
                continue
            if item == next_val:
                continue
        except StopIteration:
            pass
        yield item