简体   繁体   中英

Finding how many times a substring appears in a given string

s = 'This is a bad bad example'
sub = 'bad'

start = 0
count = 0
while True:
    start = s.find(sub,start) + 1
    if start >= 0:
        count = count+1
    else:
        break

print 'The number of times bad appears in the given string is: ' + str(count)

This is what I have tried. I have tried to debug it on my own but I am just not able to figure out where I am going wrong. Am I using the find() function incorrectly?

Shouldn't be too hard, and the best part is that python removes the need to loop over everything!

Try this:

>>> a = "this is a bad bad example"
>>> a.count('bad')
2

a.count(b) returns the number of occurrences of b in string or list a .

EDIT To address your code:

while True:
    start = s.find(sub,start) + 1
    if start >= 0:
        count = count+1
    else:
        break

You are using find() correctly, but when there are not more bad s to be found it will return -1 , which you then add one to (also correctly, due to 0-indexing) BUT you then check start >= 0 which will ALWAYS return true because -1 (a false result) will become 0 (a positive result)

Try this:

start = -1 # start at -1 now, you'll see why...
while True:
    start = s.find(sub,start + 1)
    if start >= 0:
        count = count+1
    else:
        break

So you account for off-by-one errors in the find() call rather than storing it in your termination condition. Better yet:

while start >= 0:
    start = s.find(sub,start+1)
    count += 1

I could be wrong, but I don't think count works as you might expect if the substrings "overlap". For example:

s1="dadad"
s2='dadsdad'

sub="dad"

s1.count(sub) #1
s2.count(sub) #2

This is how I worked around it:

def overlapping_substring_count(string,sub_string):
    l=len(sub_string)
    count=0
    for i in range(len(string)-len(sub_string)+1):
        if(string[i:i+len(sub_string)] == sub_string ):      
            count+=1
    return count  

s1='dadad'
s2="dad"

print(overlapping_substring_count(s1,s2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM