Reputation: 73
I need to do something similar to the CASE WHEN .. OR .. THEN from SQL in python for STRINGS. For example, if I say "DOG" or "CAT".. my translation is "ANIMAL".
I don't want to use IF ELIF ELIF..
The only solution that i can see is:
l = ['cat','dog', 'turttle']
d = {'animal': ['cat','dog', 'turttle']}
word = 'cat'
if word in l:
for i, j in d.iteritems():
if word in j:
print i
else:
print word
animal
It works but it seems very ugly..
Any other solution?
THANKS!
Upvotes: 6
Views: 24300
Reputation: 41872
what happened if I have 1M of words and translations?
An alternate approach is to store your data in a way that's convenient for defining it but before the main body of your code, invert the data (once) into a form that's more efficient for runtime:
by_kingdoms = {
'animal': {'cat', 'dog', 'turtle'},
'plant': {'rosemary', 'thyme'},
}
by_families = {}
for kingdom, families in by_kingdoms.items():
for family in families:
by_families[family] = kingdom
word = 'cat'
print(by_families[word])
This assumes well-structured data but you can even have overlap by making the values of the by_families
dictionary lists of kingdoms in which this family might appear:
from collections import defaultdict
by_kingdoms = {
'animal': {'cat', 'dog', 'turtle', 'bird of paradise'},
'plant': {'rosemary', 'thyme', 'bird of paradise'},
}
by_families = defaultdict(list)
for kingdom, families in by_kingdoms.items():
for family in families:
by_families[family].append(kingdom)
word = 'bird of paradise'
print(by_families[word])
Upvotes: 0
Reputation: 76
You can use a couple of data structure based efficiencies to scale your program as follows:
Something like this:
kingdom = {'animal':set(['Cat','Dog','Turtle']), 'plant':set(['Rosemary','Thyme'])}
word = 'cat'
for family in kingdom:
if word in kingdom[family]: # here is where constant time lookup occurs
print family
else:
print word
Alternately, you could define classes for "Animal" and "Plant", etc... depending on how much functionality is specific to the "Animal" or "Plant" stuff. I do subscribe to the principle of avoiding placeholder code, so would recommend not to look into classes unless you have reason to implement it.
Upvotes: 0
Reputation: 12417
You can do in this way:
animal_list = ['cat','dog', 'turttle']
plant_list = ['tree', 'grass']
d = {'animal': animal_list, 'plant': plant_list}
word = 'tree'
for key, value in d.iteritems():
if word in value:
print key
Upvotes: 1
Reputation: 106455
For your purpose I would suggest that you go with a dict indexed by the name of the animal instead. The list l
in your code would then also be redundant because it's simply the keys of this dict.
d = {
'cat': 'animal',
'dog': 'animal',
'turtle': 'animal'
}
word = 'cat'
print(d.get(word, word))
Upvotes: 7
Reputation: 5334
d = {'animal': ['cat','dog', 'turttle']}
word = 'cat'
if word in d['animal']:
print('animal')
Upvotes: 0