【问题标题】:Finding elements not in a list查找不在列表中的元素
【发布时间】:2011-01-07 10:32:43
【问题描述】:

这是我的代码:

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 包含一个整数列表。我想将itemz 进行比较,并打印出与item 相比时不在z 中的数字。

item 相比,我可以打印z 中的元素,但是当我尝试使用上面的代码执行相反操作时,什么也打印不出来。

有什么帮助吗?

【问题讨论】:

标签: python list


【解决方案1】:

您的代码并没有像我认为的那样做。 for item in z: 行将遍历z,每次使item 等于z 的一个元素。因此,原始的item 列表会在您对其进行任何操作之前被覆盖。

我想你想要这样的东西:

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]

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

set(item) - set(z)

【讨论】:

  • 如果检查的列表包含非唯一元素,使用set 将无法正常工作,因为set 将首先从列表中删除所有非唯一元素,但只有一次出现。
【解决方案2】:

itemz是排序迭代器的情况下,我们可以通过这样做将复杂度从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

如果两者都是迭代器,我们还有机会减少不将z (exclude_sorted_iterator) 存储为列表的内存占用。

【讨论】:

  • for 循环(这是已批准的答案)大哦是 O(n) 并且您的答案在 for 循环中有一段时间嵌套循环,因此在您的情况下复杂性会增加 O(n^2)
【解决方案3】:

使用列表推导:

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]

【讨论】:

    【解决方案4】:
    list1 = [1,2,3,4]; list2 = [0,3,3,6]
    
    print set(list2) - set(list1)
    

    【讨论】:

      【解决方案5】:

      当您遍历 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
      

      【讨论】:

        【解决方案6】:

        您的代码是无操作的。根据循环的定义,“item”必须在 Z 中。Python 中的“For ... in”循环意味着“循环通过名为 'z' 的列表,每次循环时,给我下一个项目列表,并将其称为“项目””

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

        我认为您的困惑源于您两次使用变量名“item”来表示两种不同的东西。

        【讨论】:

          【解决方案7】:

          不,z 未定义。 item 包含一个整数列表。

          我认为你想要做的是:

          #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
          

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

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:
              >>> 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])
              

              【讨论】:

                【解决方案10】:
                >> items = [1,2,3,4]
                >> Z = [3,4,5,6]
                
                >> print list(set(items)-set(Z))
                [1, 2]
                

                【讨论】:

                  猜你喜欢
                  • 2018-12-11
                  • 2012-04-11
                  • 2011-04-05
                  • 2011-01-12
                  • 2016-07-18
                  • 2013-11-21
                  • 2012-07-06
                  • 2013-06-25
                  • 2015-02-07
                  相关资源
                  最近更新 更多