python科学计算库太大_Python 科学计算 、数据库(问题汇集)
1 linear programming in python?
6Answers
active
oldest
votes
up vote9down vote
accepted
Is there a reason why you have todo it in Python?If youdo not have to then it is a lot more easier to do this ina modeling langage, see here.
If you absouletly have todo it inPython then I would suggest PyGLPK or PyMathProg.
I have beenusing GLPK for 8years now and I highly recommend it, however I have never tried these Python interfaces so I cannot help you any further with that.
shareimprovethisanswer
answered May22 '12 at 16:06
Ali25.4k8881454
+1 for "use the right tool." – djechlin May 22 '12 at 22:46
use PuLP, its an awesome pythoninterface for GLPK, CPLEX or Gurobi – Tom Larkworthy Oct 7 '13 at 20:18
2Anonymous downvotes aren't helping anybody. What is wrong with the answer? – Ali Nov 1'14 at 20:47add a comment
up vote15down vote
I'd recommend the package cvxopt for solving convex optimization problems in Python. A short example with Python code for a linear program is in cvxopt's documentation here.
detials
http://stackoverflow.com/questions/10697995/linear-programming-in-python
http://abel.ee.ucla.edu/cvxopt/examples/tutorial/lp.html
2 Python 两两互换礼物
importrandomdefselectObject(tcnt):
mapRet={}
maxTry= 1000000 ##最大尝试次数
cnt =0while(len(mapRet)
cnt+= 1can= random.randint(1, tcnt)if(len(mapRet) + 1 != can and (can not inmapRet.values())):
mapRet[len(mapRet)+ 1] =canelif cnt >maxTry:
mapRet={}
cnt=0print mapRet
View Code
3 PySAL: Python Spatial Analysis Library
http://pysal.github.io/index.html
4 Python 链接数据库
#-*- coding: utf-8 -*-
"""Created on Fri Apr 03 21:59:32 2015"""
importpyodbcfrom numpy import *cnxn= pyodbc.connect('DSN=TH;UID=root;PWD=1234')
cursor=cnxn.cursor()
ODBCDNS= ';'.join(['DRIVER={{SQL SERVER}','SERVER= localhost','DATABASE=tmall','UID=root','PWD=1234'])
cnxn=pyodbc.connect(ODBCDNS)
cursor=cnxn.cursor()
cursor.execute("select * FROM dbo.3MIN")
result= [row for row incurr]
cnxn.close()importpprint
pprint.pprint(result)
python 图像处理,数字图片识别
http://scikit-image.org/docs/dev/auto_examples/#detection-of-features-and-objects
http://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python
getattr函数,可以得到一个直到运行时才知道名称的函数的应用。>>> li = [ "Larry", "Curly"]>>>li.pop
>>> getattr( li, "pop")
>>> getattr( li, "append" )( "Moe")>>>li
['Larry', 'Curly', 'Moe']>>>从上面的代码可以看出 li.pop 等用于 getattr( li,"pop"),但是这样不是调用pop函数,真正的
的调用是getattr( li,"append" )("Moe")。下面的例子是对getattr()所代表函数的参数的分析,可以
看出假如参数超过append的参数个数将会出错>>> getattr( li, "append" )( "Moe1", "Moe2")
Traceback (most recent call last):
File"", line 1, in TypeError: append() takes exactly one argument (2given)>>> get = getattr( li, "append")>>> get( "Moe1")>>>li
['Larry', 'Curly', 'Moe', 'Moe1']>>>具体问题:
以下是 stackoverflow上提出的一个问题
have a str objectfor example: menu='install'. I want to run install method fromthis string. For example when I call menu(some, arguments) it will call install(some, arguments). Is there any way to do that ?
然后其中的一个用getattr的解答是:classMyClass(object):definstall(self):print "In install"method_name= 'install' #set by the command line options
my_cls =MyClass()
method=getattr(my_cls, method_name)if notmethod:raise Exception("Method %s not implemented" %method_name)
method()
从getattr这个问题可以看到python的一个很重要的功能自省。在《Dive into python》是这样介绍----“自省是指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作。用这种方法,你可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用事先并不知道名称的函数。”
参考资料:
stackoverflow 网站
《Dive into python》
Python getattr
Python 变换字符的进制
import binascii
binascii.b2a_hex(u"data".encode("utf8"))
python 经纬度距离和日期间隔
importmathfrom datetime importdatetimedefrad(data):return math.pi * float(data) / 180
defdislonlat(lon1, lat1, lon2, lat2):
radlat1=rad(lat1)
radlat2=rad(lat2)
a= radlat1 -radlat2
b= rad(lon1) -rad(lon2)
ret= 2 * math.asin(math.sqrt(math.pow(math.sin(a/2),2) + math.cos(radlat1) * math.cos(radlat2)*math.pow(math.sin(b/2),2)))
ret= ret * 6378.137;
ret= round(ret, 4) ## 保留四位小数
return ret * 1000 ##单位米
defdisTime(date1, date2):
one= datetime.strptime(date1, '%Y%m%d%H%M%S')
two= datetime.strptime(date2, '%Y%m%d%H%M%S')return (two-one).seconds
View Code
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
