string manipulation question

Donn Cave donn at
Sat Nov 10 18:23:55 CET 2001

Quoth "AT&T News" <wavy_gravy at>:

|  I am looking for a more resaonable way to do the following, instead of ugly
| "if", "elif" constructs. Here goes:
| I have a string (line) that might contain, for example *any* *one* of the
| patterns or words "whale" , "cat".
| How do I elegantly set a variable say "animal" depending upon what word is
| there. I.e I want
| animal = "MAMMAL" if line contains the word "whale"
| or
| animal = "FELINE" if line contains the word "cat".
| is there a elegant way to code the above construct, I have 10 of these
| patterns/keywords that can be found in a line. I thought of using a
| dictionary for the keyword patterns, but the pattern/keyword does not have
| any known context in the line.

I guess the assignment is clear enough here, but your goal has an
esthetic dimension that isn't so clear.  Can you be more specific
about ugliness and elegance?  Do it the obvious way, why not?

	Donn Cave, donn at

More information about the Python-list mailing list