string manipulation question
wavy_gravy at worldnet.att.net
Sat Nov 10 17:49:18 CET 2001
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"
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.
More information about the Python-list