Re: Is there a command that returns the number of substrings in a string?

2009-10-27 Thread Gerard Flanagan

alex23 wrote:

Gerard Flanagan grflana...@gmail.com wrote:

def count(text, *args):


Other than the ability to handle multiple substrings, you do realise
you've effectively duplicated str.count()?


I realise that calling this count function with a single argument would 
be functionally identical to calling str.count(), yes. But I can imagine 
the situation of wanting to find multiple (disjoint) substrings. Is 
there a reason for preferring multiple calls to str.count() in such a 
case? Or is there a more obvious approach?


 Gerard Flanagan grflana...@gmail.com wrote:
 re.findall?

Forget that, that was stupid.



--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a command that returns the number of substrings in a string?

2009-10-27 Thread Gabriel Genellina
En Tue, 27 Oct 2009 04:31:22 -0300, Gerard Flanagan grflana...@gmail.com  
escribió:



alex23 wrote:

Gerard Flanagan grflana...@gmail.com wrote:

def count(text, *args):

 Other than the ability to handle multiple substrings, you do realise
you've effectively duplicated str.count()?


I realise that calling this count function with a single argument would  
be functionally identical to calling str.count(), yes. But I can imagine  
the situation of wanting to find multiple (disjoint) substrings. Is  
there a reason for preferring multiple calls to str.count() in such a  
case? Or is there a more obvious approach?


There is a more efficient algorithm (Aho-Corasick) which computes all  
occurences of a set of substrings inside a given string at once. It  
*should* be faster than repeatly calling str.find for every substring, and  
faster than using regular expressions too. (note that you get not only the  
count of occurences, but their positions too).
I've seen a couple implementations for Python; if they're *actually*  
faster is to be determined...


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a command that returns the number of substrings in a string?

2009-10-27 Thread alex23
Gerard Flanagan grflana...@gmail.com wrote:
 I realise that calling this count function with a single argument would
 be functionally identical to calling str.count(), yes. But I can imagine
 the situation of wanting to find multiple (disjoint) substrings. Is
 there a reason for preferring multiple calls to str.count() in such a
 case? Or is there a more obvious approach?

No, I totally missed that it was counting disjoint substrings :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a command that returns the number of substrings in a string?

2009-10-26 Thread Gerard Flanagan

Peng Yu wrote:

For example, the long string is 'abcabc' and the given string is
'abc', then 'abc' appears 2 times in 'abcabc'. Currently, I am calling
'find()' multiple times to figure out how many times a given string
appears in a long string. I'm wondering if there is a function in
python which can directly return this information.


re.findall?

 patt = re.compile('abc')
 len(patt.findall('abcabc'))
2

For groups of non-overlapping substrings, tested only as far as you see:

8--

import re
from collections import defaultdict

def count(text, *args):

 ret = count('abcabc', 'abc')
 ret['abc']
2
 ret = count('xabcxabcx', 'abc', 'x')
 ret['abc']
2
 ret['x']
3
 ret = count('abcabc', 'abc', 'cab')
 ret['abc']
2
 ret['cab']
0
 ret = count('abcabc', 'abc', 'ab')
 ret['abc']
2
 ret['ab']
0

args = map(re.escape, args)
args.sort()
args.reverse()
pattern = re.compile('|'.join(args))
result = defaultdict(int)
def callback(match):
matched = match.group(0)
result[matched] += 1
return matched
pattern.sub(callback, text)
return result


if __name__ == '__main__':
import doctest
doctest.testmod()
8--

--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a command that returns the number of substrings in a string?

2009-10-26 Thread alex23
Gerard Flanagan grflana...@gmail.com wrote:
 def count(text, *args):

Other than the ability to handle multiple substrings, you do realise
you've effectively duplicated str.count()?
-- 
http://mail.python.org/mailman/listinfo/python-list


Is there a command that returns the number of substrings in a string?

2009-10-23 Thread Peng Yu
For example, the long string is 'abcabc' and the given string is
'abc', then 'abc' appears 2 times in 'abcabc'. Currently, I am calling
'find()' multiple times to figure out how many times a given string
appears in a long string. I'm wondering if there is a function in
python which can directly return this information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a command that returns the number of substrings in a string?

2009-10-23 Thread Stephen Hansen
On Fri, Oct 23, 2009 at 7:31 PM, Peng Yu pengyu...@gmail.com wrote:

 For example, the long string is 'abcabc' and the given string is
 'abc', then 'abc' appears 2 times in 'abcabc'. Currently, I am calling
 'find()' multiple times to figure out how many times a given string
 appears in a long string. I'm wondering if there is a function in
 python which can directly return this information.


 'abcabc'.count('abc')
2
 print ''.count.__doc__
S.count(sub[, start[, end]]) - int

Return the number of non-overlapping occurrences of substring sub in
string S[start:end].  Optional arguments start and end are interpreted
as in slice notation.

HTH,

--S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a command that returns the number of substrings in a string?

2009-10-23 Thread Erik Max Francis

Peng Yu wrote:

For example, the long string is 'abcabc' and the given string is
'abc', then 'abc' appears 2 times in 'abcabc'. Currently, I am calling
'find()' multiple times to figure out how many times a given string
appears in a long string. I'm wondering if there is a function in
python which can directly return this information.


The .count string method.

--
Erik Max Francis  m...@alcyone.com  http://www.alcyone.com/max/
 San Jose, CA, USA  37 18 N 121 57 W  AIM/Y!M/Skype erikmaxfrancis
  Diplomacy and defense are not substitutes for one another. Either
   alone would fail. -- John F. Kennedy, 1917-1963
--
http://mail.python.org/mailman/listinfo/python-list