[Python-ideas] string method count()

Neil Girdhar mistersheik at gmail.com
Mon May 7 15:56:01 EDT 2018

```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.

On Thursday, April 26, 2018 at 5:45:20 AM UTC-4, Jacco van Dorp wrote:
>
> or build it yourself...
>
> def str_count(string, sub):
>   c = 0
>   for c in range(len(string)-len(sub)):
>     if string[c:].startswith(sub):
>       c += 1
>   return c
>
> (probably some optimizations possible...)
>
> Or in one line with a generator expression:
> def str_count(string, sub):
>   return sum(string[c:].startswith(sub) for c in
> range(len(string)-len(sub)))
>
> regular expressions would probably be at least an order of magnitude
> better in speed, if it's a bottleneck to you. But pure python
> implementation for this is a lot easier than it would be for the
> current string.count().
>
> 2018-04-26 8:57 GMT+02:00 Wes Turner <wes.t... at gmail.com <javascript:>>:
> >
> >
> > On Wednesday, April 25, 2018, Steven D'Aprano <st... at pearwood.info
> <javascript:>> wrote:
> >>
> >> On Wed, Apr 25, 2018 at 11:22:24AM -0700, Julia Kim wrote:
> >> > Hi,
> >> >
> >> > There’s an error with the string method count().
> >> >
> >> > x = ‘AAA’
> >> > y = ‘AA’
> >> > print(x.count(y))
> >> >
> >> > The output is 1, instead of 2.
> >>
> >> Are you proposing that there ought to be a version of count that looks
> >> for *overlapping* substrings?
> >>
> >> When will this be useful?
> >
> >
> > "Finding a motif in DNA"
> > http://rosalind.info/problems/subs/
> >
> > This is possible with re.find, re.finditer, re.findall, regex.findall(,
> > overlapped=True), sliding window
> >
> https://stackoverflow.com/questions/2970520/string-count-with-overlapping-occurrences
> >
> > n-grams can be by indices or by value.
> > count = len(indices)
> > https://en.wikipedia.org/wiki/N-gram#Examples
> >
> >
> https://en.wikipedia.org/wiki/String_(computer_science)#String_processing_algorithms
> >
> > https://en.wikipedia.org/wiki/Sequential_pattern_mining
> >
> >>
> >>
> >> --
> >> Steve
> >> _______________________________________________
> >> Python-ideas mailing list
> >> Python... at python.org <javascript:>
> >> https://mail.python.org/mailman/listinfo/python-ideas
> >> Code of Conduct: http://python.org/psf/codeofconduct/
> >
> >
> > _______________________________________________
> > Python-ideas mailing list
> > Python... at python.org <javascript:>
> > https://mail.python.org/mailman/listinfo/python-ideas
> > Code of Conduct: http://python.org/psf/codeofconduct/
> >
> _______________________________________________
> Python-ideas mailing list
> Python... at python.org <javascript:>
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180507/928bca19/attachment-0001.html>
```