Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 384
NIlesh Sharma Avatar asked Dec 07 '22 05:12

NIlesh Sharma


2 Answers

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
like image 179
Michael Brennan Avatar answered Dec 19 '22 08:12

Michael Brennan


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']
like image 32
Marcel Wilson Avatar answered Dec 19 '22 08:12

Marcel Wilson