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
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
--
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,
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
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
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:
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
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
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 ;-)
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 =
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
11 matches
Mail list logo