mardi 10 juillet 2018

if list items not in list then

So I am wondering if there is a more "beautiful" way of doing this. Currently I have over a thousand lists list_of_lists where each individual list looks something like this:

list_of_items = ["dog", "mouse", "cow", "goat", "fish"]

Some of the lists contain other animals/strings, and don't have some of the above. It depends.

I now wish to make an if statement that says:

for x in list_of_lists:
    if "cow" is not in list_of_items and "cat" not in list_of_items:
        print("Cat or Cow could not be found in list {}".format(x)

And this does exactly what it is supposed to. If it finds either "cat" or "cow" in the current list, nothing will get printed. But if it finds neither, a print statement will occur.

My problem is that I have several "cow", "cat", and such that I needed to include in my if statement. And if I have 10, as an example, it will become kind of long and ugly to look at. So is there some way to just say: if list_of_animals not in list_of_items:, where list_of_animals would just be a list of strings that should have been included in the and statement?

Aucun commentaire:

Enregistrer un commentaire