python - Count Repeated occurence of a substring in string -


suppose have string this

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

now want find out 'available' substring has occur in string how many times repeatedly. in case should 5 `'available' coming 5 times repeatedly,it helpful if can give python library function this, regex solutions welcoming.

what have tried far

aa.count('#available') 

which giving me 6,

aa.count('#available#available') 

which again wrong.

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

is wrong

groupby itertools splendid these types of problems:

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

output:

2 booked 5 available 1 booked 1 available 1 booked 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -