python switch语句的多种实现方法
2016-12-09 11:04
537 查看
python switch语句的多种实现方法
说明:
- python中没有switch关键字
- 定义 字典
- 调用 字典的get(key)获取对应的表达式
1,实现switch的一般方式
复制代码代码示例:
from __future__ import division
def add(x, y) :
return x + y
def subtract(x, y) :
return x - y
def multiply(x, y) :
return x * y
def divide(x, y) :
return x / y
def calculate(x, y, symbol) :
if "+" == symbol :
print add(x, y)
elif "-" == symbol :
print subtract(x, y)
elif "*" == symbol :
print multiply(x, y)
elif "/" == symbol :
print divide(x, y)
else :
pass
calculate(1, 2, "+")
calculate(1, 2, "-")
calculate(1, 2, "*")
calculate(1, 2, "/")
2,字典实现switch语句
- value 为 函数
复制代码代码示例:
from __future__ import division
def add(x, y) :
return x + y
def subtract(x, y) :
return x - y
def multiply(x, y) :
return x * y
def divide(x, y) :
return x / y
calculateDict = {"+":add, "-":subtract, "*":multiply, "/":divide}
#print calculateDict["+"](1,2)
#print calculateDict.get("+")(1,2)
def calculate(x, y, symbol) :
print calculateDict.get(symbol)(x, y)
calculate(1, 2, "+")
calculate(1, 2, "-")
calculate(1, 2, "*")
calculate(1, 2, "/")
3,常用形式
复制代码代码示例:
from __future__ import division
def calculate(x, y, symbol) :
calculateDict = {
"+" : x + y ,
"-" : x - y ,
"*" : x * y ,
"/" : x / y
}
print calculateDict.get(symbol)
calculate(1, 2, "+")
calculate(1, 2, "-")
calculate(1, 2, "*")
calculate(1, 2, "/")
说明:
- python中没有switch关键字
- 定义 字典
- 调用 字典的get(key)获取对应的表达式
1,实现switch的一般方式
复制代码代码示例:
from __future__ import division
def add(x, y) :
return x + y
def subtract(x, y) :
return x - y
def multiply(x, y) :
return x * y
def divide(x, y) :
return x / y
def calculate(x, y, symbol) :
if "+" == symbol :
print add(x, y)
elif "-" == symbol :
print subtract(x, y)
elif "*" == symbol :
print multiply(x, y)
elif "/" == symbol :
print divide(x, y)
else :
pass
calculate(1, 2, "+")
calculate(1, 2, "-")
calculate(1, 2, "*")
calculate(1, 2, "/")
2,字典实现switch语句
- value 为 函数
复制代码代码示例:
from __future__ import division
def add(x, y) :
return x + y
def subtract(x, y) :
return x - y
def multiply(x, y) :
return x * y
def divide(x, y) :
return x / y
calculateDict = {"+":add, "-":subtract, "*":multiply, "/":divide}
#print calculateDict["+"](1,2)
#print calculateDict.get("+")(1,2)
def calculate(x, y, symbol) :
print calculateDict.get(symbol)(x, y)
calculate(1, 2, "+")
calculate(1, 2, "-")
calculate(1, 2, "*")
calculate(1, 2, "/")
3,常用形式
复制代码代码示例:
from __future__ import division
def calculate(x, y, symbol) :
calculateDict = {
"+" : x + y ,
"-" : x - y ,
"*" : x * y ,
"/" : x / y
}
print calculateDict.get(symbol)
calculate(1, 2, "+")
calculate(1, 2, "-")
calculate(1, 2, "*")
calculate(1, 2, "/")
相关文章推荐
- Python switch/case语句实现方法
- 再谈将C++语言源码转成html的方法(vim实现,可用于java,perl,python等等多种语言)
- C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)
- leetcode练习(204) 多种方法求质数 python实现
- 飘逸的python - 两种with语句实现方法
- 通过委托实现方法数组来代替switch分支语句
- Python核心编程多种方法实现过滤filter()
- C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)
- Python没有switch语句的解决方法
- python实现switch语句
- Python多种方法实现句子中单词倒置(好未来2017笔试题)
- python中 switch 语句的实现
- [转载] Python没有switch语句的解决方法
- python 实现switch/case语句
- Python with语句上下文管理器两种实现方法分析
- 运用Python实现多种排序的方法
- 飘逸的python - 两种with语句实现方法
- C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)
- python with语句上下文管理的两种实现方法
- Python学习(13)--Lambda表达式和switch语句的实现