optimization question

Andrew Koenig ark at research.att.com
Mon Aug 12 17:53:06 CEST 2002

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
(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.

Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark

More information about the Python-list mailing list