[Python-ideas] string method count()

Jacco van Dorp j.van.dorp at deonet.nl
Thu Apr 26 05:44:21 EDT 2018


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.turner at gmail.com>:
>
>
> On Wednesday, April 25, 2018, Steven D'Aprano <steve at pearwood.info> 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-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


More information about the Python-ideas mailing list