Finding elements not in a list

前端 未结 10 631
一向
一向 2020-11-27 13:29

So heres my code:

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<

相关标签:
10条回答
  • 2020-11-27 13:55

    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.

    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)
    
    0 讨论(0)
  • 2020-11-27 13:55

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

    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.

    0 讨论(0)
  • 2020-11-27 14:08

    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]
    
    0 讨论(0)
  • 2020-11-27 14:10

    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.

    0 讨论(0)
  • 2020-11-27 14:10

    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

    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.

    0 讨论(0)
  • 2020-11-27 14:12
    >>> 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])
    
    0 讨论(0)
提交回复
热议问题