简体   繁体   中英

Using 'not' in a 'for-loop statement'

  1. Why is not possible to use a not in a for statement? Assuming that both object and list are iterable

  2. If you can't do that is there another way around?

Here is an example, but "obviously" a syntax error:

tree = ["Wood", "Plank", "Apples", "Monkey"]

plant = ["Flower", "Plank", "Rose"]

for plant not in tree:
    # Do something
    pass 
else:
    # Do other stuff
    pass

Here's one way, using sets and assuming that both objects and list are iterable:

for x in set(objects).difference(lst):
    # do something

First of all, you should not call a variable list , that'll clash with a built-in name. Now the explanation: the expression set(objects).difference(lst) performs a set difference , for example:

lst = [1, 2, 3, 4]
objects = [1, 2, 5, 6]
set(objects).difference(lst)
=> set([5, 6])

As you can see, we found the elements in objects that are not in the list.

If objects and list are two lists, and you want to iterate over every element of objects that isn't in list , you want the following:

for object in objects:
    if object not in list:
        do_whatever_with(object)

This loops over everything in objects and only processes the ones that aren't in list . Note that this won't be very efficient; you could make a set out of list for efficient in checking:

s = set(list)
for object in objects:
    if object not in s:
        do_whatever_with(object)

It looks like you are confusing a couple of things. The for loop is used to iterate over sequences (lists, tuples, characters of a string, sets, etc). The not operator reverses boolean values. Some examples:

>>> items = ['s1', 's2', 's3']
>>> for item in items:
...   print item
...
s1
s2
s3
>>> # Checking whether an item is in a list.
... print 's1' in items
True
>>> print 's4' in items
False
>>>
>>> # Negating
... print 's1' not in items
False
>>> print 's4' not in items
True

If you mean to iterate over a list except few:

original = ["a","b","c","d","e"]
to_exclude = ["b","e"]
for item [item for item in orginal if not item in to_exclude]: print item

Produces:

a
c
d

Here is a simple way to achieve what you want:

 list_i_have = [1, 2, 4]  
 list_to_compare = [2, 4, 6, 7]

 for l in list_i_have:
     if l not in list_to_compare:
         do_something()
     else:
         do_another_thing()

Foreach item in the list you have, you can have a exclude list to check it is inside of list_to_compare.

You can also achieve this with list comprehension:

["it is inside the list" if x in (3, 4, 5) else "it is not" for x in (1, 2, 3)]

You may use list comprehension combined with inline if:

>>> lst = [1, 2, 3, 4]
>>> objects = [1, 2, 5, 6]
>>> [i for i in objects if i not in lst]
[5, 6]

And another way:

from itertools import ifilterfalse
for obj in ifilterfalse(set(to_exclude).__contains__, objects):
    # do something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM