Great suggestions, guys! Thanks so much!
And yes, I stand corrected. A better suited subject title would have
been Counting all overlapping substrings.
Thanks again,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
I wrote:
[counting all (possibly overlapping) occurences of a substring in a string]
def count_subs(s,subs,pos=0) :
pos = 1+s.find(subs,pos)
return pos and 1+count_subs(s,subs,pos)
.
now to push lisp-style to the extreme,
Azolex wrote:
Python 2.5a1 (r25a1:43589, Apr 5 2006, 10:36:43) [MSC v.1310 32 bit
(Intel)] on win32
...
cnt_ss = lambda s,ss,p=-1,n=-1 : n if np else
cnt_ss(s,ss,s.find(ss,p+1),n+1)
sarcastic-mode
Ah, the pleasure of having a ternary operator in the language!
/sarcastic-mode
Hi all,
How can one count all the permutations of a substring in a string? For
a more concrete example, let's say
targetstr = 'AAA'
and
probestr = 'AA'
I want to consider how many times one can count probestr ('AA') in
targetstr ('AAA'). The value in this example is, obviously, 2: you can
match
Chris Lasher wrote:
Hi all,
How can one count all the permutations of a substring in a string? For
a more concrete example, let's say
targetstr = 'AAA'
and
probestr = 'AA'
I want to consider how many times one can count probestr ('AA') in
targetstr ('AAA'). The value in this example is,
[EMAIL PROTECTED] wrote:
Chris Lasher wrote:
Hi all,
How can one count all the permutations of a substring in a string? For
a more concrete example, let's say
targetstr = 'AAA'
and
probestr = 'AA'
I want to consider how many times one can count probestr ('AA') in
targetstr
On Apr 5, 2006, at 5:07 PM, Chris Lasher wrote:
Hi all,
How can one count all the permutations of a substring in a string? For
a more concrete example, let's say
targetstr = 'AAA'
and
probestr = 'AA'
I want to consider how many times one can count probestr ('AA') in
targetstr ('AAA').
[counting all (possibly overlapping) occurences of a substring in a string]
def count_subs(s,subs,pos=0) :
pos = 1+s.find(subs,pos)
return pos and 1+count_subs(s,subs,pos)
or equivalently
def count_subs(s,subs)
pos,cnt = 0,0
while True :
pos = 1+s.find(subs,pos)