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
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
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With