count occurrences of given words per list in a tuple of lists

You could use sum inside a list comprehension:

example_list = (['hey', 'there', 'you', 'how', 'are', 'you'],
                ['i', 'am', 'fine', 'how', 'about', 'you'],
                ['i', 'am', 'good'])

test = ['hey', 'you']

score = [sum(s in test for s in lst) for lst in example_list]


[3, 1, 0]

Consider using a set if test is large enough.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top