Re: counting number of (overlapping) occurances

2006-03-11 Thread Fredrik Lundh
Steven D'Aprano wrote > You should always quote enough of the previous poster's message to give > context. Messages sometimes go missing in Usenet, and people won't have > the foggiest idea what you are talking about. one would think that given how many Pythoneers we now have working for google,

Re: counting number of (overlapping) occurances

2006-03-10 Thread Steven D'Aprano
On Thu, 09 Mar 2006 22:01:17 -0800, John wrote: > Thanks a lot, > > This works but is a bit slow, I guess I'll have to live with it. > Any chance this could be sped up in python? I don't know. What is it? You should always quote enough of the previous poster's message to give context. Messages

Re: counting number of (overlapping) occurances

2006-03-09 Thread Alex Martelli
Alex Martelli <[EMAIL PROTECTED]> wrote: > John <[EMAIL PROTECTED]> wrote: > > > Thanks a lot, > > > > This works but is a bit slow, I guess I'll have to live with it. > > Any chance this could be sped up in python? > > Sure (untested code): > > def count_with_overlaps(needle, haystack): >

Re: counting number of (overlapping) occurances

2006-03-09 Thread Alex Martelli
John <[EMAIL PROTECTED]> wrote: > Thanks a lot, > > This works but is a bit slow, I guess I'll have to live with it. > Any chance this could be sped up in python? Sure (untested code): def count_with_overlaps(needle, haystack): count = 0 pos = 0 while True: where = haystack.

Re: counting number of (overlapping) occurances

2006-03-09 Thread Ben Cartwright
John wrote: > This works but is a bit slow, I guess I'll have to live with it. > Any chance this could be sped up in python? Sure, to a point. Instead of: def countoverlap(s1, s2): return len([1 for i in xrange(len(s1)) if s1[i:].startswith(s2)]) Try this version, which takes smaller sl

Re: counting number of (overlapping) occurances

2006-03-09 Thread Paul Rubin
"John" <[EMAIL PROTECTED]> writes: > This works but is a bit slow, I guess I'll have to live with it. > Any chance this could be sped up in python? Whoops, I meant to say: len([1 for i in xrange(len(s1)) if s1.startswith(s2,i)]) That avoids creating a lot of small strings. If s1 is large you

Re: counting number of (overlapping) occurances

2006-03-09 Thread John
Thanks a lot, This works but is a bit slow, I guess I'll have to live with it. Any chance this could be sped up in python? Thanks once again, --j -- http://mail.python.org/mailman/listinfo/python-list

Re: counting number of (overlapping) occurances

2006-03-09 Thread Paul Rubin
"John" <[EMAIL PROTECTED]> writes: > if S1 = "" > and S2 = "AA" > > then the count is 3. Is there an easy way to do this in python? > I was trying to use the "count" function but it does not do > overlapping counts it seems. len([1 for i in xrange(len(s1)) if s1[i:].startswith(s2)]) -- http:

counting number of (overlapping) occurances

2006-03-09 Thread John
I have two strings S1 and S2. I want to know how many times S2 occurs inside S1. For instance if S1 = "" and S2 = "AA" then the count is 3. Is there an easy way to do this in python? I was trying to use the "count" function but it does not do overlapping counts it seems. Thanks, --j -- ht