Suppose I have a list of lists like this
l = [['a', 'paragraph', 'is'],
['a', 'paragraph', 'can'],
['a', 'dog', 'barks']]
also suppose I have this smaller list a = ['a', 'paragraph'] I want to count the number of occurrences different final word types succeeding the string. Therefore, the answer in this case should be 2 since ‘is’ and ‘can’ succeed the string ‘a paragraph’.
I was trying to do something like this
l.count(a)
but that did not work and gives me 0.
Ill try to spell this idea out more clearly, we basically have this substring ‘a paragraph’ and there are two occurrences that have ‘a paragraph’ namely ‘is’ and ‘can’, therefore since there is 2 unique cases the answer is 2.
>Solution :
Make a set of all the desired words:
myset = {item[2] for item in l if item[:2] == ['a', 'paragraph']}
Then use len() of the set.