Python: Count overlapping substring in a string

2020-02-06 09:53发布

Say I have string = 'hannahannahskdjhannahannah' and I want to count the number of times the string hannah occurs, I can't simply use count, because that only counts the substring once in each case.

Ie.

I am expecting to return 4 but only returns 2 when I run this in pyCharm with string.count('hannah')

5条回答
混吃等死
2楼-- · 2020-02-06 09:57

You could use a running index to fetch the next occurance:

bla = 'hannahannahskdjhannahannah'
cnt = 0
idx = 0
while True:
    idx = bla.find('hannah', idx)
    if idx >= 0:
        cnt += 1
        idx += 1
    else:
        break
print(cnt)

Gives:

>> 4
查看更多
We Are One
3楼-- · 2020-02-06 09:57

If you want to count also nonconsecutive substrings, this is the way to do it

def subword(lookup,whole):
    if len(whole)<len(lookup):
          return 0
    if lookup==whole:
          return 1
    if lookup=='':
          return 1
    if lookup[0]==whole[0]:
         return subword(lookup[1:],whole[1:])+subword(lookup,whole[1:])
    return subword(lookup,whole[1:])
查看更多
家丑人穷心不美
4楼-- · 2020-02-06 10:02

Don't want to answer this for you as it's simple enough to work out yourself.

But if I were you I'd use the string.find() method which takes the string you're looking for and the position to start looking from, combined with a while loop which uses the result of the find method as it's condition in some way.

That should in theory give you the answer.

查看更多
老娘就宠你
5楼-- · 2020-02-06 10:04
'''
s: main string
sub: sub-string
count: number of sub-strings found
p: use the found sub-string's index in p for finding the next occurrence of next sub-string
'''
count=0
p=0
for letter in s:
    p=s.find(sub,p)   
    if(p!=-1):
        count+=1
        p+=1
print count
查看更多
我想做一个坏孩纸
6楼-- · 2020-02-06 10:18

How about something like this?

>>> d = {}
>>> string = 'hannahannahskdjhannahannah'
>>> for i in xrange(0,len(string)-len('hannah')+1):
...     if string[i:i+len('hannah')] == 'hannah':
...             d['hannah'] = d.get('hannah',0)+1
... 
>>> d
{'hannah': 4}
>>> 

This searches the string for hannah by splicing the string iteratively from index 0 all the way up to the length of the string minus the length of hannah

查看更多
登录 后发表回答