Search substring in a string and get index of all occurances

2006-06-21 Thread Nico Grubert
Hi there, I would like to search for a substring in a string and get the index of all occurances. mystring = 'John has a really nice powerbook.' substr = ' ' # space I would like to get this list: [4, 8, 10, 17, 22] How can I do that without using for i in mystring which might be

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Pierre Quentel
mystring = 'John has a really nice powerbook.' substr = ' ' # space pos = 0 indices = [] while True: i = mystring.find(substr,pos) if i==-1: break indices.append(i) pos = i+1 print indices [4, 8, 10, 17, 22] Pierre --

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Fredrik Lundh
Nico Grubert wrote: I would like to search for a substring in a string and get the index of all occurances. mystring = 'John has a really nice powerbook.' substr = ' ' # space I would like to get this list: [4, 8, 10, 17, 22] the find and index methods take an optional start argument,

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Maric Michaud
Another variant, I feel this one more natural as it doesn't contain a C-looking infinite loop (also I made it a generator but this is not the topic). In [160]: def indices(s, subs) : .: last = 0 .: for ind, part in in enumerate(s.split(subs)[:-1]) : .: yield

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Fredrik Lundh
Maric Michaud wrote: Another variant, I feel this one more natural as it doesn't contain a C-looking infinite loop doing things in a convoluted way because you think that non-infinite while- loops are not natural? you can get help for that, you know ;-) Actually it's even more efficient

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread K.S.Sreeram
Maric Michaud wrote: Actually it's even more efficient than Lundh's effbot's solution finds overlapping occurrences, whereas your solution finds non-overlapping occurrences. So efficiency comparisons are not valid. e.g: indices( 'a', 'aa' ) your solution gives: 0,2 effbots's solution:

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Tim Chase
I would like to search for a substring in a string and get the index of all occurances. mystring = 'John has a really nice powerbook.' substr = ' ' # space I would like to get this list: [4, 8, 10, 17, 22] How can I do that without using for i in mystring which might be

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread bearophileHUGS
Maric Michaud: I'd love str implement a xsplit(sub, start, end) method, so I could have wrote : enumerate(s.xsplit(subs, 0, -1)). Some of such str.x-methods (or str.i-methods, etc) can be useful (especially for Py3.0), but keeping APIs simple and compact is very important, otherwise when you

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Maric Michaud
Le Mercredi 21 Juin 2006 14:01, Fredrik Lundh a écrit : Another variant, I feel this one more natural as it doesn't contain a C-looking infinite loop doing things in a convoluted way because you think that non-infinite while- loops are not natural? you can get help for that, you know ;-)

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Fredrik Lundh
K.S.Sreeram wrote: effbot's solution finds overlapping occurrences, whereas your solution finds non-overlapping occurrences. So efficiency comparisons are not valid. oops. my bad. here's a fixed version: result = []; pos = 0 try: while 1: pos =

Re: Search substring in a string and get index of all occurances

2006-06-21 Thread Fredrik Lundh
Tim Chase wrote: indicies = [i for i in xrange(len(mystring)) if mystring.startswith(substr, i)] indicies [4, 8, 10, 17, 22] is my preferred way of doing this. it's things like this that makes me wonder why I spent a week speeding up the string implementation for Python 2.5 (with