Check if all elements are in another list in Python

I want to check if reference_list

all items in a required_strings

.

required_strings = ['apple','banana','carrot']

reference_list = [['apple','tree'],
                  ['banana','grass'],
                  ['carrot','grass']]

      

I want to get true or false as test result. The expected response is "true".

This is what I tried:

test = [i for i in reference_list if any(s in i for s in required_strings)]
print test

      

+3


source to share


4 answers


You can do this using set

and itertools.chain

. We will use some set theory and consider required_strings

both reference_list

as sets and demonstrate that required_strings <= reference_list

; that is, the set of required lines is completely contained within the list of links.

Use first itertools.chain

to flatten the small list.

from itertools import chain
chain(*reference_list) # Iterable object

      

Then include both sets and the checked list in the sets and compare to see if one set is completely contained in the other.



from itertools import chain
set(required_strings) <= set(chain(*reference_list))

      

If you do not use chain

, you can use sum(list_of_lists, [])

to reduce it.

set(required_strings) <= set(sum(reference_lists, []))

      

However, I highly recommend using set

instead list

, as this problem is better suited for set

. You don't need to import it either; you can simply use the class in the same way as you list

.

+4


source


There is no reason to try anything supernatural or concise until you have a working solution that you understand.

Try to make your answer as similar as possible to the English specification of the problem first. Basically you have to iterate over the strings in the list you want and check if they are in any of the subscriptions. Here's a stub that can be made by a function.



for req_string in required_strings:
   appears_in_reference = False
   for sub_list in reference_list:
       if req_string in sub_list:
           appears_in_reference = True
           break
   if not appears_in_reference:
       return False

return True

      

+1


source


Python 2 This is one way to do it:

import itertools
required_strings = ['apple','banana','carrot']

reference_list = [['apple','tree'],
                  ['banana','grass'],
                  ['carrot','grass']]
x = list(itertools.chain(*reference_list))
print all(l in x for l in required_strings )

      

Output:

True

      

0


source


Here is the understanding that the bools list returns:

>>> test = [(True if i in s else False) for i in required_strings for s in reference_list]
[True, False, False, False, True, False, False, False, True]

      

Alternatively, use generator comprehension to check the length of the original list of required strings. Note the use of () instead of [].

>>> test = ((True if i in s else False) for i in required_strings for s in reference_list)

      

Then check it for length:

>>> counter = 0
>>> for truthiness in test:
...     if truthiness == True:
...         counter += 1
>>> len(required_strings) == counter
True

      

Edit: This will probably only work in Python 3.4+, but I thought it was worth doing.

0


source







All Articles