[Python-ideas] An iterable version of find/index for strings?

Tom Schumm phong at phong.org
Mon Apr 8 16:08:21 CEST 2013


On Sun, April 07, 2013 11:58:09 PM Andrew Barnert wrote:
> > * Strings are usually short and always entirely in memory - the iterator
> > requirement isn't commonplace.
> This, I think, is a better point. If you need iterfind, there's a good
> chance you're going to want to replace the string with an mmap, an iterator
> around read, something that generates the string on the fly, etc. There
> will be _some_ programs for which str.iterfind is more useful than a
> generic iterfind function, but maybe not that many...

The big advantage in my use is that the iterator makes code shorter and more 
readable, and I can use it in list comprehensions.

Maybe it's not useful enough to be a str method; perhaps something for 
itertools or more-itertools? If it was rewritten to use index() instead of 
find() it could be generalized for lists and such. Or maybe I'll just leave it 
in my recipe box with my trees and memoization decorators.

Then again, if I'm doing lots and lots of linear searches, I feel like I've 
not thought about the problem long enough...

-- 
Tom Schumm
http://www.fwiffo.com/



More information about the Python-ideas mailing list