Efficient way of testing for substring being one of a set?
tinnews at isbd.co.uk
tinnews at isbd.co.uk
Thu Apr 3 08:15:13 EDT 2008
Jeff <jeffober at gmail.com> wrote:
> def foo(sample, strings):
> for s in strings:
> if sample in s:
> return True
> return False
>
> This was an order of magnitude faster for me than using str.find or
> str.index. That was finding rare words in the entire word-list (w/
> duplicates) of War and Peace.
However it's the wrong way around, in my case 'sample' is the longer
string and I want to know if s is in it. It's simple enough to do it
the other way around though:-
def foo(sample, strings):
for s in strings:
if s in sample:
return True
return False
Using in rather than find() and making it a function would seem to be
the way to go, thanks.
--
Chris Green
More information about the Python-list
mailing list