Scanning a file
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
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'
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')
More information about the Python-list