Python的switch方法
来源:互联网 发布:zookeeper java开发 编辑:程序博客网 时间:2024/06/13 06:42
C/C++程序员习惯用switch语句,但python是没有的。那么python如何处理多分支情况呢?方法一是使用if--elif--elif--else,显然分支越多,判断越多,不是最优;那么下面介绍最优的方法二——使用字典映射来实现switch/case方法。
>>> #-*- coding: utf-8 -*->>> import time>>> now = time.localtime()>>> print nowtime.struct_time(tm_year=2017, tm_mon=9, tm_mday=10, tm_hour=10, tm_min=38, tm_sec=43, tm_wday=6, tm_yday=253, tm_isdst=0)>>> print time.strftime('%Y-%m-%d %H:%M:%S',now)2017-09-10 10:38:43>>> def long_time_cn(t):print time.strftime('%Y-%m-%d %H:%M:%S',t)>>> def long_time_en(t):print time.strftime('%B %d %Y %H:%M:%S',t)>>> def short_time_cn(t):print time.strftime('%y-%#m-%#d %#H:%#M',t)>>> def short_time_en(t):print time.strftime('%b %#d %y %#H:%#M',t)>>> timelist = {'cnl':long_time_cn,'enl':long_time_en,'cns':short_time_cn,'ens':short_time_en}>>> def mytime(outputmothod,outputtime):try:t = timelist.get(outputmothod)(outputtime) return texcept Exception,e:print '%s' %(e)>>> mytime('cnl',now)2017-09-10 10:38:43>>> mytime('cns',now)17-9-10 10:38>>> mytime('ens',now)Sep 10 17 10:38>>> mytime('enl',now)September 10 2017 10:38:43>>> mytime('ennnn',now)'NoneType' object is not callable>>> mytime(1,now)'NoneType' object is not callable>>>
阅读全文
0 0
- Python的switch方法
- python:一种代替 switch--case 的方法
- python switch语句的多种实现方法
- python switch的实现
- Python的switch实现
- python的switch实现
- Python的Switch语句
- switch的处理方法
- python 带default的switch
- python 中switch的实现
- python switch结构的实现
- python中switch的用法
- Python switch/case语句实现方法
- 通过Mininet Python API 添加自定义交换机 switch=user的方法
- python switch
- python switch
- Python没有switch语句的解决方法
- Python实现类似switch的分支结构
- linux下安装python3.5
- hdu 2546 饭卡 (01背包)
- shell 函数
- 675. Cut Off Trees for Golf Event
- cookie学习笔记-通过数组读取cookie
- Python的switch方法
- R语言多线程运算(解决R循环慢的问题)
- CashRegSys
- mave工程自学初探
- oracle 多表查询
- 用 C# 写 TCP socket 连接
- 开发中用到的正则表达式
- 在vs2017中如何配置opengl
- 洛谷 P1131 [ZJOI2007]时态同步