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

Mitja nun at example.com
Thu Nov 25 17:15:11 EST 2004


On Thu, 25 Nov 2004 15:43:53 +0100, Nico Grubert <nicogrubert at arcor.de>  
wrote:

> Example:
> I have the following string: "test and testing and not perl or testit or  
> example"
>
> I want to convert this string to:
> '*test*' and '*testing*' and not '*perl*' or '*testit*' or '*example*'

A compact, though not too readable a solution:

foo="test and testing and not perl or testit or example"

' '.join([
   ("'*"+w+"*'",w)[w in ('and','or')]
   for w in foo.split()
]).replace("and '*not*'","and not")

-- 
Mitja



More information about the Python-list mailing list