switch-case statement for STRINGS in Python
Asked Answered
C

5

6

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!

Culpable answered 17/8, 2018 at 13:16 Comment(0)
C
7

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))
Caligula answered 17/8, 2018 at 13:20 Comment(4)
.. but, what happened if I have 1M of words and translations?Culpable
dict would still be the quickest implementation because it allows for an average lookup time of O(1). You should always use what you want to look up with as the key, not the other way around.Caligula
For dict, Get Item is O(1) "average case" and O(n) "Amortized Worst Case". I was hoping for a solution more like in C using jump tables. source: wiki.python.org/moin/TimeComplexityBritt
@avatarofhope2, that is a very unlikely worst case, like winning the lottery n weeks in a row.Jaques
M
1

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
Margay answered 17/8, 2018 at 13:29 Comment(3)
THANKS, it's similar to my solution.. but, what happened if I have 1M of words and translations?Culpable
It could be a bit slower(i havent tried to compare with blhsing), but avoid to use huge dictionaryMargay
On this solution, assuming the animal_list and plant_list do not have duplicate elements, you can use a set rather than a list. Sets hash their elements so lookup time is O(1). The time taken will be the same whether you have 3 elements or 1 million.Microstructure
A
0
d = {'animal': ['cat','dog', 'turttle']}

word = 'cat'

if word in d['animal']:
    print('animal')
Allantoid answered 17/8, 2018 at 13:22 Comment(0)
M
0

You can use a couple of data structure based efficiencies to scale your program as follows:

  1. Use a dictionary to store the data of what classifies as "animal" or other
  2. Use a set rather than a list to group by classification. This allows for constant time lookup, regardless of how big the set is.

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.

Microstructure answered 17/8, 2018 at 15:48 Comment(0)
S
0

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])
Scandalize answered 17/8, 2018 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.