Python 使用dict实现switch的操作
作者:KeeJee 时间:2021-12-28 02:47:52
Python3还是没有switch,可以利用if-else来实现,但是非常不方便。使用dict来实现会比较简洁优雅。
# -*- coding: utf-8 -*-
"""
Python利用dict实现switch
"""
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):
assert(y != 0)
return x / y
mapping = {"+": add, "-": subtract, "*": multiply, "/": divide}
def cal(x, y, symbol="+"):
assert(symbol in mapping)
return mapping.get(symbol)(x, y)
if __name__ == "__main__":
result = cal(3, 0, "&")
补充:python 字典dict实现switch case【实际应用】(非dict.get()方法实现)
看了不少帖子,几乎都是采用字典的.get()方法实现,据说有个弊端:“会将字典每个带括号的方法都执行一遍”。
以下方法可避免该弊端,并可以传参。如有不足请指正!
#!/usr/bin/python3
# conf_cmd = conf_items["cmd"].split(":")[0]
test_no = "T1"
#test_no = "T2"
#test_no = "T3"
id = 1
def test1(id):
print("test1:%d" % id)
def test2(id):
print("test2")
def test3(id):
print("test3")
funcs = {"T1": test1,
"T2": test2,
"T3": test3}
try:
func = funcs[test_no]
func(id)
except Exception:
pass
输出:
test1:1
补充:Python实现类似switch的分支结构
switch语句相信大家都很熟悉,而且swith语句表达的分支结构比if...elif...else语句表达更清晰,代码的可读性更高,但是在Python中,却没有提供这一个关键字。那我们该如何通过其他方式来实现这类似的结构呢?
虽然没有switch语句,但是我们可以通过Python中的dict即字典来实现类似switch结构的方法
实现代码如下:
def operator(o,x,y):
result={
'+' : x+y,
'-' : x-y,
'*' : x*y,
'/' : x/y
}
print(result.get(o))
oper=input()//接收从键盘输入的数据
operator(oper,4,2)
运行效果如下所示:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/ZK_J1994/article/details/78593955
标签:Python,dict,switch
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Matplotlib绘制扇形图标签重叠问题解决过程
2022-06-27 05:22:48
![](https://img.aspxhome.com/file/2023/5/103625_0s.png)
交互设计:简单
2011-08-27 16:46:27
一文搞懂Python中列表List和元组Tuple的使用
2022-10-09 02:24:58
![](https://img.aspxhome.com/file/2023/8/93968_0s.png)
基于Python共轭梯度法与最速下降法之间的对比
2022-10-27 06:58:12
![](https://img.aspxhome.com/file/2023/5/65885_0s.jpg)
python 将日期戳(五位数时间)转换为标准时间
2021-09-14 06:36:47
![](https://img.aspxhome.com/file/2023/5/82525_0s.png)
解释FCKeditor样式的工作原理
2007-10-11 13:34:00
Pycharm连接远程服务器过程图解
2023-07-25 04:37:53
![](https://img.aspxhome.com/file/2023/1/88991_0s.png)
python提取word文件中的图片并上传阿里云OSS
2021-02-01 10:52:25
![](https://img.aspxhome.com/file/2023/4/107964_0s.png)
基于Python使用永中文档转换服务的方式
2021-09-29 12:26:12
![](https://img.aspxhome.com/file/2023/7/95127_0s.png)
python实现BP神经网络回归预测模型
2023-01-21 17:53:44
![](https://img.aspxhome.com/file/2023/2/98322_0s.png)
一文搞懂Python中Pandas数据合并
2023-03-21 13:28:56
![](https://img.aspxhome.com/file/2023/3/80393_0s.png)
如何利用Python实现简单C++程序范围分析
2022-07-19 00:32:48
Python连接Mssql基础教程之Python库pymssql
2022-01-24 04:47:20
python中的内置函数getattr()介绍及示例
2023-01-15 19:16:46
Python按钮的响应事件详解
2023-12-24 15:35:08
![](https://img.aspxhome.com/file/2023/5/98825_0s.png)
PHP入门教程之会话控制技巧(cookie与session)
2023-11-16 00:13:39
Python爬取豆瓣视频信息代码实例
2021-10-28 06:41:46
![](https://img.aspxhome.com/file/2023/3/84993_0s.jpg)
Python3使用Matplotlib 绘制精美的数学函数图形
2021-09-21 17:38:45
![](https://img.aspxhome.com/file/2023/8/90428_0s.jpg)
布同 Python中文问题解决方法(总结了多位前人经验,初学者必看)
2021-01-02 08:46:49
PDO::errorInfo讲解
2023-06-06 01:51:53