【发布时间】:2016-02-05 21:18:13
【问题描述】:
我有一系列的条件句:
if ':' in particle:
do something
elif 'eq' in particle:
do something else
elif 'lt' in particle:
do another thing
elif 'le' in particle:
etc.
elif 'gt' in particle:
etc., etc.
elif 'ge' in particle:
etc., etc., etc.
elif 'ne' in particle:
more etc.
我想使用字典映射模式来实现这一点,但是键有问题。
我试过了:
def case_evaluator(particle):
switcher = {
':' in particle: do something,
'eq' in particle: do something else,
'lt' in particle: do another thing,
...
}
return switcher.get(particle, "nothing")
但是,我一直“什么都没有”。东西怎么可能一无所获?
这看起来应该很简单,但是唉......
【问题讨论】:
-
不应该在某处声明“参数”吗?
-
对不起...我刚刚编辑了这个。
标签: python dictionary switch-statement conditional mapper