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

Jean Brouwers mrjean1ATcomcastDOTnet at no.spam.net
Thu Nov 25 13:49:36 EST 2004


Just a comment.  The w.strip() call in the last line is superfluous in
this particular case.  The items in the list resulting from the
query.split() call will be stripped already.  Example,

  >>> "a    b      c".split()
  ['a', 'b', 'c']


/Jean Bouwers


In article <co4s42$mkb$04$1 at news.t-online.com>, Diez B. Roggisch
<deetsNOSPAM at web.de> wrote:

> import sets
> KEYWORDS = sets.Set(['and', 'or', 'not'])
> 
> query = "test and testing and not perl or testit or example"
> 
> def decorate(w):
>     if w in KEYWORDS:
>         return w
>     return "*%s*" % w
> 
> query = " ".join([decorate(w.strip()) for w in query.split()])



More information about the Python-list mailing list