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

Peter Hansen peter at engcorp.com
Sat Nov 27 12:54:02 EST 2004


Peter Maas wrote:
> Diez B. Roggisch schrieb:
>> import sets
>> KEYWORDS = sets.Set(['and', 'or', 'not'])
>>...
>> def decorate(w):
>>     if w in KEYWORDS:
>>         return w
>>     return "*%s*" % w
>>
> 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.)

-Peter



More information about the Python-list mailing list