Search substring in a string and get index of all occurances

Maric Michaud maric at aristote.info
Wed Jun 21 09:08:01 EDT 2006


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 ;-)

Hehe, I was looking for a more pythonic solution (one like Tim gave), but not 
convinced.

Sure these loops are common and natural. They're so natural I wonder why I 
avoid this kind of code :

for i in lst :
	...
	if continue_condition : continue
	...
	if break_condition : break
	...

Maybe I'll vote for a goto statment for Python3000...

Really I prefer code like this, when it's possible :

for i in [ e for e in lst[:first_breaking_elt] if not e in 
continue_condition ] :
	....

> and a potentially large number of new strings.  there's a lot of string
> copying going on in there...

Oh, yes, you're right, a xsplit operator would be of minor help as it will 
copy all the string piece by piece.

Le Mercredi 21 Juin 2006 14:27, K.S.Sreeram a écrit :
> > Actually it's even more efficient than Lundh's
>
> effbot's solution finds overlapping occurrences, whereas your solution
> finds non-overlapping occurrences.
Missed that.

> So efficiency comparisons are not valid. 
Right, but anyway, the problem is more about memory usage.

Regards,

-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list