Replacing words from strings except 'and' / 'or' / 'and not'
Nico Grubert
nicogrubert at arcor.de
Thu Nov 25 11:01:26 EST 2004
> def decorate(s):
> words = s.split()
> # make "and not" a 'word'
> for i in range(len(words)-1,-1,-1):
> if words[i] == 'and' and words[i+1] == 'not':
> words[i:i+2] = ["and not"]
> special = ('and', 'and not', 'or')
> for word in words:
> if word in special:
> yield word
> else:
> yield "*%s*" % word
>
> s = "test and testing and not perl or testit or example"
> print " ".join(decorate(s))
>
>Hoping I didn't just do your homework, I wish you a Happy Thanksgiving...
>
>
Hi Skip,
thanks a lot. That's it.
Your code is much more compact and easier to read than my first try that
was around 100 lines of code without
having a 100% solution. Nice to see, how simple Python can be.
Kind regards,
Nico
More information about the Python-list
mailing list