[ python-Feature Requests-850408 ] Count in String with overlap support (code included)

SourceForge.net noreply at sourceforge.net
Fri Nov 28 12:49:41 EST 2003


Feature Requests item #850408, was opened at 2003-11-27 14:31
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=850408&group_id=5470

Category: Python Interpreter Core
Group: None
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Sebastian Bassi (sbassi)
Assigned to: Nobody/Anonymous (nobody)
Summary: Count in String with overlap support (code included)

Initial Comment:
Hello,

The string.count function doesn't support overlapping
string. Here is a new function I'd like to see included
in Python, a string.overcount function that has
overlapping support. This is useful for dna related
work and I think it has another uses.
The "standar" string.count work like this:

>s=newstring('aaabbaa')
>s.count("aa")
 2

But the "REAL" result, should be 3, becuase there are 3
instances of "aa".

So, to get the 2 as a result, you will use count, the
get 3, you will have overcount (or whatever you want to
call it).

So here is my code:

class newstring(str):
  def overcount(self,pattern):
    """Returns how many p on s, works for overlapping"""
    ocu=0
    x=0
    while 1:
      try:
        i=self.index(pattern,x)
      except ValueError:
        break
      ocu+=1
      x=i+1
    return ocu

s=newstring('aaabbaa')
print s.overcount('aa')

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-11-28 12:49

Message:
Logged In: YES 
user_id=80475

Your pure python snippet is well done and may make a good
ASPN recipe; however, I do not think it sufficiently
unversal to warrant including it in C.  I can think of no
non-toy real world use cases for regular text. It seems most
applicable to abstract character strings and even there
counting overlapping sequences is not a common requirement.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=850408&group_id=5470



More information about the Python-bugs-list mailing list