查找不在列表中的元素。

时间:2021-08-02 07:41:31

So heres my code:

所以这是我的代码:

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

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

Z contains a list of integers. I want to compare item to Z and print out the numbers that are not in Z when compared to item. I can print the elemtens that are in Z when compared not items, but when i try and do the opposite using the code above nothing prints.

Z包含一个整数列表。我想把项目和Z进行比较,然后把不在Z中的数字和项目进行比较。我可以打印出Z中的元素,但当我尝试用上面的代码做相反的事情时,没有打印。

Any help?

任何帮助吗?

10 个解决方案

#1


98  

Your code is not doing what I think you think it is doing. The line for item in z: will iterate through z, each time making item equal to one single element of z. The original item list is therefore overwritten before you've done anything with it.

你的代码没有做我认为它正在做的事情。z:中的项目行将遍历z,每次使项目等于z的单个元素。因此,在您使用它之前,原始项目列表将被覆盖。

I think you want something like this:

我想你想要这样的东西:

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

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

But you could easily do this like:

但是你可以这样做:

[x for x in item if x not in z]

or (if you don't mind losing duplicates of non-unique elements):

或者(如果您不介意丢失非唯一元素的重复):

set(item) - set(z)

#2


45  

>> items = [1,2,3,4]
>> Z = [3,4,5,6]

>> print list(set(items)-set(Z))
[1, 2]

#3


9  

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

print set(list2) - set(list1)

#4


9  

Using list comprehension:

使用列表理解:

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

or using filter function :

或使用过滤器功能:

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

Using set in any form may create a bug if the list being checked contains non-unique elements, e.g.:

如果所检查的列表包含非唯一元素,例如:

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 list comprehension as above

与上面列出的理解

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

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

or filter function:

或过滤功能:

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

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

#5


3  

If you run a loop taking items from z, how do you expect them not to be in z? IMHO it would make more sense comparing items from a different list to z.

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

#6


2  

>>> item = set([0,1,2,3,4,5,6,7,8,9])
>>> z = set([2,3,4])
>>> print item - z
set([0, 1, 5, 6, 7, 8, 9])

#7


2  

No, z is undefined. item contains a list of integers.

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

I think what you're trying to do is this:

我认为你想做的是:

#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

As has been stated in other answers, you may want to try using sets.

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

#8


2  

Your code is a no-op. By the definition of the loop, "item" has to be in Z. A "For ... in" loop in Python means "Loop though the list called 'z', each time you loop, give me the next item in the list, and call it 'item'"

您的代码是禁止操作的。根据循环的定义,“item”必须在Z. A中。在“Python中的循环”中,循环的意思是“循环遍历名为‘z’的列表,每次循环时,给我列表中的下一项,并将其称为‘item’”

http://docs.python.org/tutorial/controlflow.html#for-statements

http://docs.python.org/tutorial/controlflow.html的语句

I think your confusion arises from the fact that you're using the variable name "item" twice, to mean two different things.

我认为你的困惑源于你两次使用变量名"item"来表示两个不同的东西。

#9


0  

You are reassigning item to the values in z as you iterate through z. So the first time in your for loop, item = 0, next item = 1, etc... You are never checking one list against the other.

当你在z中迭代时,你重新分配项目到z中的值。你永远不会检查一个列表和另一个列表。

To do it very explicitly:

非常明确地说:

>>> 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

#10


0  

In the case where item and z are sorted iterators, we can reduce the complexity from O(n^2) to O(n+m) by doing this

如果项目和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

If the two are iterators, we also have the opportunity to reduce the memory footprint not storing z (exclude_sorted_iterator) as a list.

如果这两个是迭代器,我们还可以减少不将z (exclusive de_sorted_iterator)存储为列表的内存占用。

#1


98  

Your code is not doing what I think you think it is doing. The line for item in z: will iterate through z, each time making item equal to one single element of z. The original item list is therefore overwritten before you've done anything with it.

你的代码没有做我认为它正在做的事情。z:中的项目行将遍历z,每次使项目等于z的单个元素。因此,在您使用它之前,原始项目列表将被覆盖。

I think you want something like this:

我想你想要这样的东西:

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

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

But you could easily do this like:

但是你可以这样做:

[x for x in item if x not in z]

or (if you don't mind losing duplicates of non-unique elements):

或者(如果您不介意丢失非唯一元素的重复):

set(item) - set(z)

#2


45  

>> items = [1,2,3,4]
>> Z = [3,4,5,6]

>> print list(set(items)-set(Z))
[1, 2]

#3


9  

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

print set(list2) - set(list1)

#4


9  

Using list comprehension:

使用列表理解:

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

or using filter function :

或使用过滤器功能:

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

Using set in any form may create a bug if the list being checked contains non-unique elements, e.g.:

如果所检查的列表包含非唯一元素,例如:

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 list comprehension as above

与上面列出的理解

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

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

or filter function:

或过滤功能:

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

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

#5


3  

If you run a loop taking items from z, how do you expect them not to be in z? IMHO it would make more sense comparing items from a different list to z.

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

#6


2  

>>> item = set([0,1,2,3,4,5,6,7,8,9])
>>> z = set([2,3,4])
>>> print item - z
set([0, 1, 5, 6, 7, 8, 9])

#7


2  

No, z is undefined. item contains a list of integers.

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

I think what you're trying to do is this:

我认为你想做的是:

#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

As has been stated in other answers, you may want to try using sets.

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

#8


2  

Your code is a no-op. By the definition of the loop, "item" has to be in Z. A "For ... in" loop in Python means "Loop though the list called 'z', each time you loop, give me the next item in the list, and call it 'item'"

您的代码是禁止操作的。根据循环的定义,“item”必须在Z. A中。在“Python中的循环”中,循环的意思是“循环遍历名为‘z’的列表,每次循环时,给我列表中的下一项,并将其称为‘item’”

http://docs.python.org/tutorial/controlflow.html#for-statements

http://docs.python.org/tutorial/controlflow.html的语句

I think your confusion arises from the fact that you're using the variable name "item" twice, to mean two different things.

我认为你的困惑源于你两次使用变量名"item"来表示两个不同的东西。

#9


0  

You are reassigning item to the values in z as you iterate through z. So the first time in your for loop, item = 0, next item = 1, etc... You are never checking one list against the other.

当你在z中迭代时,你重新分配项目到z中的值。你永远不会检查一个列表和另一个列表。

To do it very explicitly:

非常明确地说:

>>> 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

#10


0  

In the case where item and z are sorted iterators, we can reduce the complexity from O(n^2) to O(n+m) by doing this

如果项目和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

If the two are iterators, we also have the opportunity to reduce the memory footprint not storing z (exclude_sorted_iterator) as a list.

如果这两个是迭代器,我们还可以减少不将z (exclusive de_sorted_iterator)存储为列表的内存占用。