<div dir="ltr">Regular expressions are not just "an order of magnitude better"—they're asymptotically faster.  See https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm for a non-regular-expression algorithm.<div><br>On Thursday, April 26, 2018 at 5:45:20 AM UTC-4, Jacco van Dorp wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">or build it yourself...<p>def str_count(string, sub):<br>  c = 0<br>  for c in range(len(string)-len(sub)):<br>    if string[c:].startswith(sub):<br>      c += 1<br>  return c</p><p>(probably some optimizations possible...)</p><p>Or in one line with a generator expression:<br>def str_count(string, sub):<br>  return sum(string[c:].startswith(sub) for c in range(len(string)-len(sub)))</p><p>regular expressions would probably be at least an order of magnitude<br>better in speed, if it's a bottleneck to you. But pure python<br>implementation for this is a lot easier than it would be for the<br>current string.count().</p><p>2018-04-26 8:57 GMT+02:00 Wes Turner <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="bHhjxVLvAgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">wes.t...@gmail.com</a>>:<br>><br>><br>> On Wednesday, April 25, 2018, Steven D'Aprano <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="bHhjxVLvAgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">st...@pearwood.info</a>> wrote:<br>>><br>>> On Wed, Apr 25, 2018 at 11:22:24AM -0700, Julia Kim wrote:<br>>> > Hi,<br>>> ><br>>> > There’s an error with the string method count().<br>>> ><br>>> > x = ‘AAA’<br>>> > y = ‘AA’<br>>> > print(x.count(y))<br>>> ><br>>> > The output is 1, instead of 2.<br>>><br>>> Are you proposing that there ought to be a version of count that looks<br>>> for *overlapping* substrings?<br>>><br>>> When will this be useful?<br>><br>><br>> "Finding a motif in DNA"<br>> <a href="http://rosalind.info/problems/subs/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Frosalind.info%2Fproblems%2Fsubs%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF034aoeCJNWAxPD6KrNA7E5ksVqg';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Frosalind.info%2Fproblems%2Fsubs%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF034aoeCJNWAxPD6KrNA7E5ksVqg';return true;">http://rosalind.info/problems/<wbr>subs/</a><br>><br>> This is possible with re.find, re.finditer, re.findall, regex.findall(,<br>> overlapped=True), sliding window<br>> <a href="https://stackoverflow.com/questions/2970520/string-count-with-overlapping-occurrences" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F2970520%2Fstring-count-with-overlapping-occurrences\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEDZ1bqUsJU_Y1rwQnGpsS5ky2tjA';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F2970520%2Fstring-count-with-overlapping-occurrences\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEDZ1bqUsJU_Y1rwQnGpsS5ky2tjA';return true;">https://stackoverflow.com/<wbr>questions/2970520/string-<wbr>count-with-overlapping-<wbr>occurrences</a><br>><br>> n-grams can be by indices or by value.<br>> count = len(indices)<br>> <a href="https://en.wikipedia.org/wiki/N-gram#Examples" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FN-gram%23Examples\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGfUfq--wdiufIWk275mBbGJV30gQ';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FN-gram%23Examples\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGfUfq--wdiufIWk275mBbGJV30gQ';return true;">https://en.wikipedia.org/wiki/<wbr>N-gram#Examples</a><br>><br>> <a href="https://en.wikipedia.org/wiki/String_(computer_science)#String_processing_algorithms" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FString_(computer_science)%23String_processing_algorithms\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF9ZNi3aFYWBXAGHKxhaP6l_BEpuw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FString_(computer_science)%23String_processing_algorithms\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF9ZNi3aFYWBXAGHKxhaP6l_BEpuw';return true;">https://en.wikipedia.org/wiki/<wbr>String_(computer_science)#<wbr>String_processing_algorithms</a><br>><br>> <a href="https://en.wikipedia.org/wiki/Sequential_pattern_mining" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSequential_pattern_mining\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFErnmQe0vYBCd90huaFyJJP4ZpIA';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSequential_pattern_mining\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFErnmQe0vYBCd90huaFyJJP4ZpIA';return true;">https://en.wikipedia.org/wiki/<wbr>Sequential_pattern_mining</a><br>><br>>><br>>><br>>> --<br>>> Steve<br>>> ______________________________<wbr>_________________<br>>> Python-ideas mailing list<br>>> <a href="javascript:" target="_blank" gdf-obfuscated-mailto="bHhjxVLvAgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a><br>>> <a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>>> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a><br>><br>><br>> ______________________________<wbr>_________________<br>> Python-ideas mailing list<br>> <a href="javascript:" target="_blank" gdf-obfuscated-mailto="bHhjxVLvAgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a><br>> <a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a><br>><br>______________________________<wbr>_________________<br>Python-ideas mailing list<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="bHhjxVLvAgAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a><br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a><br></p><p></p><p></p><p></p><p></p></blockquote></div></div>