十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
Python中是沒有switch的, 所以有時我們需要用switch的用法, 就只能通過if else來實現(xiàn)了. 但if else寫起來比較冗長,
在鳳山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè),鳳山網(wǎng)站建設(shè)費(fèi)用合理。
這時就可以使用Python中的dict來實現(xiàn), 比switch還要簡潔. 用法如下:
如果是key1的情況就執(zhí)行func1, 如果是key2的情況就執(zhí)行func2...(func1, func2...所有的函數(shù)的參數(shù)形式需要相同),
假設(shè)各個函數(shù)參數(shù)均為(arg1, arg2):
dictName = {"key1":func1, "key2":func2, "key3":func3"...}#字典的值直接是函數(shù)的名字,不能加引號dictName[key](arg1, arg2)
示例代碼如下:
#!/usr/bin/python#File: switchDict.py#Author: lxw#Time: 2014/10/05import redef add(x, y): return x + ydef sub(x, y): return x - ydef mul(x, y): return x * ydef div(x, y): return x / ydef 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: passif __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
-------------------------['1', '+', '2']-------------------------
3
3PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.4 - 9
-------------------------['4', '-', '9']-------------------------
-5
-5PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.6 / 5
-------------------------['6', '/', '5']-------------------------
1
1PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 9 9
-------------------------['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
-------------------------['1', '(', '9']-------------------------PS J:\
個人感覺, 如果想用switch來解決某個問題, 并且每種情況下的操作在形式上是相同的(如都執(zhí)行某個函數(shù)并且這些函數(shù)有
相同的參數(shù)), 就可以用這種方法來實現(xiàn).
使用sicpy.io即可.sicpy.io提供了兩個函數(shù)loadmat和savemat,非常方便.
以前也有一些開源的庫(pymat和pymat2等)來做這個事,
不過自從有了numpy和scipy以后,這些庫都被拋棄了.
下面是一個簡單的測試程序,具體的函數(shù)用法可以看幫助文檔:
[python] view plaincopy在CODE上查看代碼片派生到我的代碼片
import scipy.io as sio
import matplotlib.pyplot as plt
import numpy as np
#matlab文件名
matfn=u'E:/python/測試程序/162250671_162251656_1244.mat'
data=sio.loadmat(matfn)
plt.close('all')
xi=data['xi']
yi=data['yi']
ui=data['ui']
vi=data['vi']
plt.figure(1)
plt.quiver( xi[::5,::5],yi[::5,::5],ui[::5,::5],vi[::5,::5])
plt.figure(2)
plt.contourf(xi,yi,ui)
plt.show()
sio.savemat('saveddata.mat', {'xi': xi,'yi': yi,'ui': ui,'vi': vi})
Python具備動態(tài)導(dǎo)入module并且執(zhí)行其中代碼的能力,所以你只要import你保存的東西就可以,無需中斷你當(dāng)前的代碼執(zhí)行流。