网站建设资讯

NEWS

网站建设资讯

Python编写存储函数 python存储并导入函数模块

python 字典可以储存函数吗

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/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来解决某个问题, 并且每种情况下的操作在形式上是相同的(如都执行某个函数并且这些函数有

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

如何在python中读写和存储matlab的数据文件

使用sicpy.io即可.sicpy.io提供了两个函数loadmat和savemat,非常方便.

以前也有一些开源的库(pymat和pymat2等)来做这个事,

不过自从有了numpy和scipy以后,这些库都被抛弃了.

下面是一个简单的测试程序,具体的函数用法可以看帮助文档:

[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如何将生成的代码/函数保存进文件并导入

Python具备动态导入module并且执行其中代码的能力,所以你只要import你保存的东西就可以,无需中断你当前的代码执行流。


本文题目:Python编写存储函数 python存储并导入函数模块
网站链接:http://cdweb.net/article/dojjieo.html