How many times specific list is found in a list of lists

Question:

I have the following list

a = ['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges']

and the following list of lists

b = [['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges'], ['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges', 'Pear', 'Apple'], ['Oranges', 'Strawberry', 'Pear'], ... ]

As you can see, inside of b it’s possible to have

  1. A list that’s exactly the same as a
  2. A list with the same items as a but in a different order
  3. Similar to 1 and 2 but more items than a
  4. Similar to 1 and 2 but less items than a
  5. A totally different list than a

Considering that using

for value in b:
    print(value)

one is able to get each list of b which can then be compared to a, how to know how many times cases 1, 2 and 3 occur (including duplicates)?


Inspired in this answer, I experimented

count_matches = 0

for value in b:
    ff = str(value).strip("[]")
    gg = str(a).strip("[]")
    if gg in ff:
        count_matches += 1

print(count_matches)

but this didn’t work due to the order (for instances, other items could have been added in the middle).

Answers:

In order to solve it I combined this approach with the for loop to get the value, as in

count_matches = 0
for value in b:
    if set(a) <= set(value):
        count_matches += 1
        print("a is in b")  
Answered By: Tiago Martins Peres
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.