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

SourceForge.net noreply at sourceforge.net
Thu Nov 27 14:31:34 EST 2003


Feature Requests item #850408, was opened at 2003-11-27 16:31
Message generated for change (Tracker Item Submitted) made by Item Submitter
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: Open
Resolution: None
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')

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

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