Python 科学计算 、数据库(问题汇集)
1 linear programming in python?

6 Answers active oldest votes up vote 9 down vote accepted Is there a reason why you have to do it in Python? If you do not have to then it is a lot more easier to do this in a modeling langage, see here. If you absouletly have to do it in Python then I would suggest PyGLPK or PyMathProg. I have been using GLPK for 8 years now and I highly recommend it, however I have never tried these Python interfaces so I cannot help you any further with that. shareimprove this answer answered May 22 '12 at 16:06 Ali 25.4k888145 4 +1 for "use the right tool." – djechlin May 22 '12 at 22:46 use PuLP, its an awesome python interface for GLPK, CPLEX or Gurobi – Tom Larkworthy Oct 7 '13 at 20:18 2 Anonymous downvotes aren't helping anybody. What is wrong with the answer? – Ali Nov 1 '14 at 20:47 add a comment up vote 15 down 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.
http://stackoverflow.com/questions/10697995/linear-programming-in-python
http://abel.ee.ucla.edu/cvxopt/examples/tutorial/lp.html
2 Python 两两互换礼物

import random def selectObject(tcnt): mapRet = {} maxTry = 1000000 ##最大尝试次数 cnt = 0 while(len(mapRet) < tcnt): cnt += 1 can = random.randint(1, tcnt) if(len(mapRet) + 1 != can and (can not in mapRet.values())): mapRet[len(mapRet) + 1] = can elif cnt > maxTry: mapRet = {} cnt = 0 print mapRet
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 """ import pyodbc from 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 in curr] cnxn.close() import pprint 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 <built-in method pop of list object at 0xb76b364c> >>> getattr( li, "pop" ) <built-in method pop of list object at 0xb76b364c> >>> 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 "<stdin>", line 1, in <module> TypeError: append() takes exactly one argument (2 given) >>> get = getattr( li, "append" ) >>> get( "Moe1" ) >>> li ['Larry', 'Curly', 'Moe', 'Moe1'] >>> 具体问题: 以下是 stackoverflow上提出的一个问题 have a str object for example: menu='install'. I want to run install method from this string. For example when I call menu(some, arguments) it will call install(some, arguments). Is there any way to do that ? 然后其中的一个用getattr的解答是: class MyClass(object): def install(self): print "In install" method_name = 'install' # set by the command line options my_cls = MyClass() method = getattr(my_cls, method_name) if not method: raise Exception("Method %s not implemented" % method_name) method() 从getattr这个问题可以看到python的一个很重要的功能自省。在《Dive into python》是这样介绍----“自省是指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作。用这种方法,你可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用事先并不知道名称的函数。” 参考资料: stackoverflow 网站 《Dive into python》
Python 变换字符的进制
import binascii binascii.b2a_hex(u"data".encode("utf8"))
python 经纬度距离和日期间隔

import math from datetime import datetime def rad(data): return math.pi * float(data) / 180 def dislonlat(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 ##单位米 def disTime(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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步