optimization question
Bengt Richter
bokr at oz.net
Mon Aug 12 21:43:54 CEST 2002
On Mon, 12 Aug 2002 15:53:06 GMT, Andrew Koenig <ark at research.att.com> wrote:
>Paul> I don't understand. If i and j are far apart and t is very short--
>Paul> in particular, if j-i > len(t) and j < len(s), then s[i:j]==t is
>Paul> always false.
>
>Right. In the application I was thinking of, i and j are determined by
>one part of the program and t is determined in another part, and they
>don't talk to each other. So I have three choices:
>
> 1) Write s[i:j]==t and be aware that the comparison might be
> expensive even if t is short;
>
> 2) Write s[i:j]==t, secure in the knowledge that the implementation
> will do the comparison without extracting the substring; or
>
> 3) Write (len(t)==j-1 and s[i:j]==t), assuming, of course, that
> i and j have the same sign.
>
>I've learned that (2) is not an option, so now I have to decide between
ISTM (2) could be an option if you could arrange for s to be an instance
of a class of your own design. (E.g., you write s.__getitem__(self, index)
so that it returns yet another object (with s,i,j refs in it) that will
participate in the '==' the way you'd like). You can then be secure,
because the objects behave according to your own implementation ;-)
>(1) and (3) -- a decision I'll make when I have to make it.
>
>Paul> Do you mean you want to search for t in that range of s? You could
>Paul> use the regexp module for that.
>
>Only if s and t are strings of characters, which they might not be.
>Anyway, what I'm trying to do is not as simple as searching for t in
>the range of s.
>
Regards,
Bengt Richter
More information about the Python-list
mailing list