0

I have a str and a list of str and wanted to count the number of times that list occurs in the str. How can i solve this??

I've tried this:

def count_from_word_list(s,l):
    """(str,list of str) -> int
    Return the total number of times l appears in the s
    """
    counter = 0

    for item in s.split():
        for item in l:
            if s == l:
                counter= counter + 1
    return counter  
2
  • have you tried using Counter? Commented Oct 25, 2016 at 4:13
  • yes i have it doesn't work Commented Oct 25, 2016 at 20:39

1 Answer 1

1

Simply use container_string.count(contained_string) to get a count of how many times a string contains another string!

For example:

>>> 'foofoofoo'.count('foo')
3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.