【发布时间】:2018-08-17 13:16:53
【问题描述】:
我需要做一些类似于 CASE WHEN .. OR .. THEN from SQL in python for STRINGS 的事情。例如,如果我说“DOG”或“CAT”……我的翻译是“ANIMAL”。
我不想使用 IF ELIF ELIF..
我能看到的唯一解决方案是:
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
它可以工作,但看起来很丑..
还有其他解决方案吗?
谢谢!
【问题讨论】:
标签: python dictionary switch-statement case type-synonyms