Replacing words from strings except 'and' / 'or' / 'and not'

Peter Hansen peter at engcorp.com
Sat Nov 27 14:19:43 EST 2004


Jp Calderone wrote:
> On Sat, 27 Nov 2004 12:54:02 -0500, Peter Hansen <peter at engcorp.com> wrote:
>>Sets are implemented using dictionaries, so the "if w in KEYWORDS"
>>part would be O(1) instead of O(n) as with lists...
>>
> And yet... using sets here is slower in every possible case:
[snip results]
> This is a pretty clear example of premature optimization.  

Boy, talk about preaching to the converted! ;-)

It's actually pretty silly to talk about optimizing in the case
of a list or Set or dict with only three items...  my response was
an attempt to provide an explanation for why a Set was used
instead of a list.  In other words, an attempt to read someone's
mind.  I choose what was to me at the time the most obvious
apparent reason for the choice, but I may have chosen wrong.

Let's try again:  the code is more readable with a Set because
it becomes clear to a reader that it is a simple set of items
rather than a sequence.  The ordering doesn't matter, in the
mind of the programmer, but merely whether an item is in the
set or not.  To one who is used to using Sets (I'm not yet),
I would imagine that the choice made here is the more natural
one...

(If one wanted to optimize, one might actually be better off
analyzing the frequency of usage of the three items in the set,
and converting to a list with the most-frequently used items at
the start, but then that would still be a pretty silly thing
to do for only three items, so leaving it as a Set would likely
be the best thing for overall readability.)

-over-analyses-R-us-ly yr's,
  Peter



More information about the Python-list mailing list