Wrong result in str.count() method
Question:
I’m confusing with a very simple string count operation:
s = 'BANANA'
s.count('ANA')
This should result in 2, right? Since the substring, ANA
appears 2 times in BANANA
.
But I’ve got 1 as a result.
>>> s = 'BANANA'
>>> s.count('ANA')
1
No idea why the wrong result. It is such a simple operation!
Appreciate any help.
PS: How can I solve this problem?
Answers:
string.count()
does not count the overlapping occurrences.
If you would like to count overlapped occurrences, a simple loop over the string will count it:
s = 'BANANA'
i = 0
cnt = 0
while True:
i = s.find('ANA', i)
if i >= 0:
i += 1
cnt += 1
else:
break
Alternatively you can use regex
too as in @Henrique’s answer below.
In ‘BANANA’, there is only a single complete ‘ANA’. Count() is returning 1 because after it finds ‘ANA’, all that is left is ‘NA’.
Solved the problem using the new regex
library. It has a new parameter overlapped
— extremely useful.
>>> import regex as re
>>> len(re.findall("ANA", "BANANA", overlapped=True))
2
I found the solution at this question here in SO.
I’m confusing with a very simple string count operation:
s = 'BANANA'
s.count('ANA')
This should result in 2, right? Since the substring, ANA
appears 2 times in BANANA
.
But I’ve got 1 as a result.
>>> s = 'BANANA'
>>> s.count('ANA')
1
No idea why the wrong result. It is such a simple operation!
Appreciate any help.
PS: How can I solve this problem?
string.count()
does not count the overlapping occurrences.
If you would like to count overlapped occurrences, a simple loop over the string will count it:
s = 'BANANA'
i = 0
cnt = 0
while True:
i = s.find('ANA', i)
if i >= 0:
i += 1
cnt += 1
else:
break
Alternatively you can use regex
too as in @Henrique’s answer below.
In ‘BANANA’, there is only a single complete ‘ANA’. Count() is returning 1 because after it finds ‘ANA’, all that is left is ‘NA’.
Solved the problem using the new regex
library. It has a new parameter overlapped
— extremely useful.
>>> import regex as re
>>> len(re.findall("ANA", "BANANA", overlapped=True))
2
I found the solution at this question here in SO.