摘自網絡python
python自己沒有switch語句,解決方法有如下3種:網絡
#coding: utf-8
from __future__ import division def jia(x,y): print x+y def jian(x,y): print x-y def cheng(x,y): print x*y def chu(x,y): print x/y operator = {'+':jia,'-':jian,'*':cheng,'/':chu} def f(x,o,y): operator.get(o)(x,y) f(3,'+',2)
result = {
'a': lambda x: x * 5,
'b': lambda x: x + 7,
'c': lambda x: x - 2
}[value](x)