注意! 运行Lagrange软件的指南随后贴出,现在的版本仅供参考。 How to run Lagrange in Windows XP "Lagrange is a Python package implementing likelihood models for geographic range evolution on phylogenetic trees, with methods for inferring rates of dispersal and local extinction and ancestral ranges. " http://code.google.com/p/lagrange/ 1 Download and install Python NumPy and SciPy Lagrange requires Python, NumPy and SciPy, the first step is to download and install these software. Download and install Python2.5 , because SciPy requires this version, it can be downloaded from http://www.python.org/download/ Download and install NumPy and SciPy ,you can down load theexe filesform http://sourceforge.net/project/showfiles.php?group_id=27747 for SciPy http://sourceforge.net/project/showfiles.php?group_id=1369package_id=175103 for NumPi It is strongly recommended that you download the numpy-1.2.1-win32-superpack-python2.5.exe , since the imcompatible problems may exist if you installed a incorrect version. You can visit scipy's website for more detail. http://www.scipy.org/ 2 Copy the Lagrange modules to working directory Assume you have had all the software that Lagrange required installed on your PC. You may seed a directory under C:\Python25 now unzip your lagrange2 .0.1.tar.gz copy all the file in the directory which just unzipped to C:\python25\ 3 Data preparation The python scripts for exercise can be downloaded at http://bodegaphylo.wikispot.org/Biogeography_%28Donoghue_%26_Smith%29 copy the test python script named psychotria.py and psychotria.matrix to python directory, C:\python25\ . 4 Run python scripts in cmd The python scripts can be implement in commander just which just like the DOS , to run the script, begin here: Beginrun type cmd a commander window will appeared just type cd C:\python25\ to change your working directory to test\ and type psychotria.pyresults.txt “results.txt” allowed you to save the results of the scripts in “results.txt” When all are got ready , the python script will be run in your PC
按 :这三天密集监考,没有大块的时间干正事,就一口气写了5篇《 复杂网络分析库NetworkX学习笔记 》,看点击量估计会有朋友有兴趣学习这个库,但可能很多朋友都没有接触过Python。我以前也说过,Python一点也不难学,可以说是我接触过的最简单的编程语言了(我前后学用过C、C++、C#、Basic四门语言,现在则主要用Python)。为了方便感兴趣的朋友学习Python,我上网搜索了一些Python教程,大厚本的语法书估计各位都比较忙,也没时间看,我就专找那种速成性的,还真找到一些:有《 24小时学会Python 》、《 1小时学会Python 》,还有这个《 10分钟学会Python 》(英文原版见 http://www.korokithakis.net/tutorials/python )。咱就来最快的吧:10分钟学会它! 以下是转载的全文(向原作者和译者表示感谢): 1. 准备工作 哦,你是要学习Python编程语言但是又苦于找不到一个简洁但是全面 的教程么?这个教程就是要试图在10分钟内让你掌握Python。可能它有点不像一个教程,或者说应该介于教程和cheatsheet[可以快速查找的一 个简单表单,不知道怎么翻译,译注]之间,所以在这里我只能向你展示一些最基本的概念,旨在让你能够快速入门。显然,如果你真要学习一门编程语言,你需要 使用它编码一段时间。我假定你已经有一些熟知的编程知识,因此在这里我就不再讲那些与语言无关的编程知识。教程中的关键字我都让它高亮显示,这样你就可以 一眼就看清楚。另外,为了保持教程的简洁,一些知识就只在代码中展示,只有一些简单的注释。 2. 特性 Python 是一个强类型(也就是类型都是强制指定的),动态,隐式类型的(即,你不需要声明变量),大小写敏感(var和VAR是两个不同的变量),面向对象(一切 皆是对象)的语言。[专业术语翻译有点别扭,译注] 3. 语法 Python 没有命令结束标志,并且,使用缩进来区分程序块。块开始的时候缩进开始,块结束的时候缩进结束。声明需要一个:来引领一个缩进块。注释使用#开始并且只占 一行。多行注释使用多行字符串。赋值使用等号(=),测试是否相等使用两个等号(==)。可以分别使用+=或者-=来增加或者减少变量值。这在多 个数据结构上都适用,包括字符串。你也可以在一行上使用多个变量。举例来说: myvar = 3 myvar += 2 myvar -= 1 This is a multiline comment. The following lines concatenate the two strings. mystring = Hello mystring += world. print mystring Hello world. # This swaps the variables in one line(!). myvar, mystring = mystring, myvar 4. 数据类型 在Python中可用的数据类型有列表、元组以及字典。在集合库中集合 也可用。列表就像是以维数组(但是你还可以有列表的列表),字典就是关联数组(或者叫哈希表),元组就是不可变一维数组(Python中数组可以是任何类 型,因此你可以在列表、字典或者元组中混合使用数字、字符串等数据类型)。在所有的数组类型中第一个元素的标号都是0,负数表示从后向前数,-1则表示最 后一个元素。变量可以指向函数。用法如下: sample = , (a, tuple)] mylist = mylist = List item 1 again mylist = 3.14 mydict = {Key 1: Value 1, 2: 3, pi: 3.14} mydict = 3.14 mytuple = (1, 2, 3) myfunction = len print myfunction(mylist) 你可以使用:取到数组的一个范围,:前留空则表示从第一个元素开始,:后留空则表示直到最 后一个元素。负值表示从后索引(即-1是最后一个元素)。如下所示: mylist = print mylist print mylist print mylist print mylist 5. 字符串 字符串可以使用单引号或者双引号,你可以在使用一个引号的里面嵌套使用另一种引号(也就是说,He said 'hello'.是合法的)。多行字符串则使用三引号(单双皆可)。Python还可以让你设置Unicode编码,语法如下:uThis is a unicode string.使用值填充一个字符串的时候可以使用%(取模运算符)和一个元组。每个%s使用元组中的一个元素替换,从左到右。你还可以使用字典。如下 所示: print Name: %snNumber: %snString: %s % (myclass.name, 3, 3 * -) Name: Poromenos Number: 3 String: --- strString = This is a multiline string. # WARNING: Watch out for the trailing s in %(key)s. print This %(verb)s a %(noun)s. % {noun: test, verb: is} This is a test. 6. 流程控制 流程控制使用while,if,以及for。没有select[和哪种语言对比?不知道。译 注],使用if代替。使用for来列举列表中的元素。要得到一个数字的列表,可以使用range(number)。这些声明的语法如下: rangelist = range(10) print rangelist for number in rangelist: # Check if number is one of # the numbers in the tuple. if number in (3, 4, 7, 9): # Break terminates a for without # executing the else clause. break else: # Continue starts the next iteration # of the loop. It's rather useless here, # as it's the last statement of the loop. continue else: # The else clause is optional and is # executed only if the loop didn't break. pass # Do nothing if rangelist == 2: print The second item (lists are 0-based) is 2 elif rangelist == 3: print The second item (lists are 0-based) is 3 else: print Dunno while rangelist == 1: pass 7. 函数 函数使用def关键字。可选参数在必须参数之后出现,并且可以被赋一默认值。对命名参数而言,参数名参数 名被赋一个值。函数可以返回一个元组(打开元组你就可以实现返回多个值)。Lanbda函数是个特例,它由一个表达式构成。参数使用引用传递,但是可变类 型(元组,列表,数字,字符串等等)不能被改变。举例如下: # arg2 and arg3 are optional, they have default values # if one is not passed (100 and test, respectively). def myfunction(arg1, arg2 = 100, arg3 = test): return arg3, arg2, arg1 ret1, ret2, ret3 = myfunction(Argument 1, arg3 = Named argument) # Using print with multiple values prints them all, separated by a space. print ret1, ret2, ret3 Named argument 100 Argument 1 # Same as def f(x): return x + 1 functionvar = lambda x: x + 1 print functionvar(1) 8. 类 Python部分支持类的多重继承。私有变量和方法可以使用至少两个_开始并且至少一个_结束来声明,比 如__spam(这只是约定,语言中并没有强制规定)。我们可以给类的实例赋任意的变量。请看下例: class MyClass: common = 10 def __init__(self): self.myvariable = 3 def myfunction(self, arg1, arg2): return self.myvariable # This is the class instantiation classinstance = MyClass() classinstance.myfunction(1, 2) 3 # This variable is shared by all classes. classinstance2 = MyClass() classinstance.common 10 classinstance2.common 10 # Note how we use the class name # instead of the instance. MyClass.common = 30 classinstance.common 30 classinstance2.common 30 # This will not update the variable on the class, # instead it will create a new one on the class # instance and assign the value to that. classinstance.common = 10 classinstance.common 10 classinstance2.common 30 MyClass.common = 50 # This has not changed, because common is # now an instance variable. classinstance.common 10 classinstance2.common 50 # This class inherits from MyClass. Multiple # inheritance is declared as: # class OtherClass(MyClass1, MyClass2, MyClassN) class OtherClass(MyClass): def __init__(self, arg1): self.myvariable = 3 print arg1 classinstance = OtherClass(hello) hello classinstance.myfunction(1, 2) 3 # This class doesn't have a .test member, but # we can add one to the instance anyway. Note # that this will only be a member of classinstance. classinstance.test = 10 classinstance.test 10 9. 异常处理 Python中的异常处理使用try-except 程序块: def somefunction(): try: # Division by zero raises an exception 10 / 0 except ZeroDivisionError: print Oops, invalid. fnExcept() Oops, invalid. 10. 包的导入 使用import 导入外部包,你也可以为单独的函数使用from import 这种形式来导入。下面是一个例子: import random from time import clock randomint = random.randint(1, 100) print randomint 64 11. 文件I/O Python有一个很大的内建库数组来处理文件的读写。下面的例子展示如何使用Python的文件I/O来序列化(使用 pickle把数据结构转换成字符串)。 import pickle mylist = # Open the file C:binary.dat for writing. The letter r before the # filename string is used to prevent backslash escaping. myfile = file(rC:binary.dat, w) pickle.dump(mylist, myfile) myfile.close() myfile = file(rC:text.txt, w) myfile.write(This is a sample string) myfile.close() myfile = file(rC:text.txt) print myfile.read() 'This is a sample string' myfile.close() # Open the file for reading. myfile = file(rC:binary.dat) loadedlist = pickle.load(myfile) myfile.close() print loadedlist 12. 其他 条 件语句可以链接使用。1 a 3检查a是否介于1和3之间。 可以使用del删除数组中的元素或者变量。 List comprehensions[不知怎么翻译,译注]提供一个强大的方法来创建和操作list(列表)。它由一个后跟一个for语句的表达式组成,这个 for语句后跟一个或者多个if@ 或者 @for语句,就像这样: lst1 = lst2 = print print # Check if an item has a specific property. # any returns true if any item in the list is true. any(i % 3 for i in ) True # Check how many items have this property. sum(1 for i in if i == 3) 3 del lst1 print lst1 del lst1 全局变量可以声明在函数外边并且不需要任何特殊 的声明就可以直接读取使用。但是如果你要改变该全局变量的值,你必须在函数的开始使用global关键字声明它,否则,Python将创建一个局部变量并 给他赋值(要特别注意这点,不知道的话很容易犯错)。比如: number = 5 def myfunc(): # This will print 5. print number def anotherfunc(): # This raises an exception because the variable has not # been assigned to before printing. Python knows that it a # value will be assigned to it later and creates a new, local # number instead of accessing the global one. print number number = 3 def yetanotherfunc(): global number # This will correctly change the global. number = 3 13. 结束语 这个教程并没有列出Python的全部细节(甚至连一个子集都算不上)。Python还有一系列的库以及许多其他功能,这就需要你通 过其他途径来学习了,比如很优秀的在线教程Dive into Python。我只希望这个教程能让你对Python快速上手。如果你觉得有什么需要改进或者添加的地方,或者你想看到的其他任何东西(类,错误处理,任 何东西),那就在这里留言吧。