Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count the number of occurrences different final word types succeeding the string

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading