Skip to content Skip to sidebar Skip to footer

Counting Matching Substrings In A String

s = 'abobabobabob' total = 0 for i in range(len(s)): if s[i-1 : i+2] == 'bob': total += 1 print ('times bob occurs is:' + str(total)) Is there a simpler way to chang

Solution 1:

Your if-statement is looking at a subset of s. To sort of answer your other question, here's a simpler approach that changes more than the if-statement:

This is python's regular expression library

import re

The embedded statement searches for all, non-overlapping instances of 'bob' and returns a list with each match as an element; the outer statement just counts the number of elements in the list

len(re.findall('bob',s))

Solution 2:

The following code searches for the beginning index of each 'bob' substring, and adds that value to an array. The total variable just returns the count of the values in that array.

As a one-liner:

total = 0
s = "abobabobabob"

total = len([i for i inrange(len(s)) if s.find('bob', i) == i])

print('times bob occurs is: ' + str(total))

Prints:

times bob occurs is: 3

--- Here is an alternative if you want a modification to your for loop:

total = 0
s = "abobabobabob"for i inrange(len(s)):
    if (s.find('bob', i) == i):
        total += 1print('times bob occurs is: ' + str(total))

Prints:

times bob occurs is: 3

Solution 3:

if s[i-1 : i+2] == 'bob' is checking whether the current index -1 to current index + 2 is 'bob'. It will cause issue, since i begin at 0 and i-1 is the last element of list

try:

s = "abobabobabob"
total = 0for i in range(1,len(s)):
    if s[i-1 : i+2] == 'bob':
        total += 1

there is a better way in two line

s= "abobabobabob"printsum(  s[i:i+3] =='bob'for i inrange(len(s)-2) )

3

Solution 4:

Using enumerate()

s = "abobabobabob"
n = len([i for i, w inenumerate(s) if s[i:i+3] == "bob"])

print ('times bob occurs is:', n)

This checks for every character in string s(by index i) if the character + two characters on the right equals "bob".


About your secundary question:

In your example:

s[i-1 : i+2]

refers to the index (i) of the characters in the string, where s[0] is the first character, while s[i-1 : i+2] is a slice of the string s, from the current character -1 (the one on the left) to the current character +2 (the second character on the right).

Post a Comment for "Counting Matching Substrings In A String"