NIlesh Sharma
NIlesh Sharma

Reputation: 5655

Count Repeated occurence of a substring in string

Suppose I have a string like this

aa = 'booked#booked#available#available#available#available#available#booked#available#booked'

Now I want to find out that 'available' substring has occur in this string how many times repeatedly. So in this case it should be 5 as `'available' is coming 5 times repeatedly,it will be much helpful if someone can give a python library function itself to get this, regex solutions are also welcoming.

what I have tried so far is

aa.count('#available')

which is obviously giving me 6,

aa.count('#available#available')

which is again wrong.

import re
count(re.findall('available#'))

is also wrong

Upvotes: 4

Views: 2633

Answers (2)

Marcel Wilson
Marcel Wilson

Reputation: 4572

Here is how I did it in 2.7.

import re
aa = 'booked#booked#available#available#available#available#available#booked#available#booked'
len(re.findall('#available', aa))

I think you can safely remove the pound sign based on the pattern you provided.

>>> stuff = re.findall('available', aa)
>>> stuff
['available', 'available', 'available', 'available', 'available', 'available']

Upvotes: 1

user861537
user861537

Reputation:

Groupby from itertools is splendid for these types of problems:

from itertools import groupby
aa = 'booked#booked#available#available#available#available#available#booked#available#booked'
words = aa.split('#')
for key, group in groupby(words):
    print len(list(group)), key

Output:

2 booked
5 available
1 booked
1 available
1 booked

Upvotes: 14

Related Questions