Scanning a file

Bengt Richter bokr at oz.net
Sat Oct 29 09:27:34 CEST 2005


On 28 Oct 2005 06:51:36 -0700, "pinkfloydhomer at gmail.com" <pinkfloydhomer at gmail.com> wrote:

>First of all, this isn't a text file, it is a binary file. Secondly,
>substrings can overlap. In the sequence 0010010 the substring 0010
>occurs twice.
>
ISTM you better let others know exactly what you mean by this, before
you use the various solutions suggested or your own ;-)

a) Are you talking about bit strings or byte strings?
b) Do you want to _count_ overlapping substrings??!!
Note result of s.count on your example:

 >>> s = '0010010'
 >>> s.count('0010')
 1

vs. brute force counting overlapped substrings (not tested beyond what you see ;-)

 >>> def ovcount(s, sub):
 ...     start = count = 0
 ...     while True:
 ...         start = s.find(sub, start) + 1
 ...         if start==0: break
 ...         count += 1
 ...     return count
 ...
 >>> ovcount(s, '0010')
 2

Regards,
Bengt Richter



More information about the Python-list mailing list