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

Skip Montanaro skip at pobox.com
Sun Nov 28 10:47:18 EST 2004


    >> > Is there a reason to use sets here? I think lists will do as well.
    >> 
    >> Sets are implemented using dictionaries, so the "if w in KEYWORDS"
    >> part would be O(1) instead of O(n) as with lists...
    >> 
    >> (I.e. searching a list is a brute-force operation, whereas
    >> sets are not.)

    Jp>   And yet... using sets here is slower in every possible case:
    ...
    Jp>   This is a pretty clear example of premature optimization.

I think the set concept is correct.  The keywords of interest are best
thought of as an unordered collection.  Lists imply some ordering (or at
least that potential).  Premature optimization would have been realizing
that scanning a short list of strings was faster than testing for set
membership and choosing to use lists instead of sets.

Skip



More information about the Python-list mailing list