Python中是没有switch的, 所以有时我们需要用switch的用法, 就只能通过if else来实现了. 但if else写起来比较冗长,

这时就可以使用Python中的dict来实现, 比switch还要简洁. 用法如下:

  如果是key1的情况就执行func1, 如果是key2的情况就执行func2...(func1, func2...所有的函数的参数形式需要相同),

假设各个函数参数均为(arg1, arg2):

dictName = {"key1":func1, "key2":func2, "key3":func3"...}  #字典的值直接是函数的名字,不能加引号
dictName[key](arg1, arg2)

  示例代码如下:

#!/usr/bin/python
#File: switchDict.py
#Author: lxw
#Time: 2014/10/05 import re def add(x, y):
return x + y def sub(x, y):
return x - y def mul(x, y):
return x * y def div(x, y):
return x / y def main():
inStr = raw_input("Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.\n")
inList = re.split("(\W+)", inStr)
inList[1] = inList[1].strip()
print("-------------------------")
print(inList)
print("-------------------------") #Method 1:
if inList[1] == "+":
print(add(int(inList[0]), int(inList[2])))
elif inList[1] == "-":
print(sub(int(inList[0]), int(inList[2])))
elif inList[1] == "*":
print(mul(int(inList[0]), int(inList[2])))
elif inList[1] == "/":
print(div(int(inList[0]), int(inList[2])))
else:
pass #Method 2:
try:
operator = {"+":add, "-":sub, "*":mul, "/":div}
print(operator[inList[1]](int(inList[0]), int(inList[2])))
except KeyError:
pass if __name__ == '__main__':
main()

Output:

PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
1 + 2
-------------------------
['', '+', '']
-------------------------
3
3
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
4 - 9
-------------------------
['', '-', '']
-------------------------
-5
-5
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
6 / 5
-------------------------
['', '/', '']
-------------------------
1
1
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
1 9 9
-------------------------
['', '', '', ' ', '']
-------------------------
PS J:\> python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.
1 ( 9
-------------------------
['', '(', '']
-------------------------
PS J:\>

  个人感觉, 如果想用switch来解决某个问题, 并且每种情况下的操作在形式上是相同的(如都执行某个函数并且这些函数有

相同的参数), 就可以用这种方法来实现.

最新文章

  1. 建筑材料系统 ASP.NET MVC4.0 + WebAPI + EasyUI + Knockout 的架构设计开发
  2. 原生JS默认设置默认值的写法
  3. TCP协议承载的DNS报文,DNS报文首部前多出两个字节的DNS报文长度字段,是何意义?
  4. TFS2012常见问题及解答
  5. 高性能javascript
  6. 基于cocos2d-x的游戏框架设计——李成
  7. datagrid合并行列--并不能影响序号列内容...(formatter的锅.)
  8. linux下面测试网络带宽 (转载)
  9. USB系列之一:列出你的USB设备
  10. iOS面试题04-runtime
  11. IT科技企业逻辑思维面试题
  12. Django1.11搭建一个简易上传显示图片的后台
  13. [LeetCode] Student Attendance Record I 学生出勤记录之一
  14. patch 请求时,关于id的报错问题
  15. 分布式系列四: HTTP及HTTPS协议
  16. jmeter 之beanshell preprocessor
  17. 注解@CrossOrigin解决跨域问题
  18. 《剑指offer》-双栈实现队列
  19. mysql配置主从复制和常见问题
  20. 深入浅出 - vue变化侦测原理

热门文章

  1. Android JNI和NDK学习(03)--动态方式实现JNI(转)
  2. NGINX扩展
  3. (html)前端如何验证token的合法性来判断用户是否登录?
  4. mysql workbench 导出表结构
  5. spring boot 多层级mapper
  6. java的集合层次图
  7. is char signed or unsigned?
  8. ashx后门
  9. 从C#到Swift,Swift学习笔记
  10. php 防止刷新重复下载文件