Python語(yǔ)法速覽與機(jī)器學(xué)習(xí)開發(fā)環(huán)境搭建
Python
Python 是一門高階、動(dòng)態(tài)類型的多范式編程語(yǔ)言。人生苦短,請(qǐng)用Python,大量功能強(qiáng)大的語(yǔ)法糖的同時(shí)讓很多時(shí)候Python代碼看上去有點(diǎn)像偽代碼。譬如我們用Python實(shí)現(xiàn)的簡(jiǎn)易的快排相較于Java會(huì)顯得很短小精悍:
- def quicksort(arr):
- if len(arr) <= 1:
- return arr
- pivot = arr[len(arr) / 2]
- left = [x for x in arr if x < pivot]
- middle = [x for x in arr if x == pivot]
- right = [x for x in arr if x > pivot]
- return quicksort(left) + middle + quicksort(right)
- print quicksort([3,6,8,10,1,2,1])
- # Prints "[1, 1, 2, 3, 6, 8, 10]"
Python 版本
Python社區(qū)存在的最大的問(wèn)題就是版本分裂,這也是筆者一直覺(jué)得有點(diǎn)雞肋般的感覺(jué),畢竟對(duì)于處女座而言實(shí)在是難受。目前Python社區(qū)中存在兩個(gè)不同的主要版本:2.7與3.4。Python 3.0引入了很多不向后兼容的變化,因此很多遵循2.7版本的代碼并不能適用于3.4版本。我們可以使用python --version命令來(lái)查看當(dāng)前使用的版本。
常用習(xí)慣
模塊 注意點(diǎn) 換行 反斜杠()繼續(xù)上一行,Python文件以模塊形式組織。Python程序語(yǔ)句不以分號(hào)結(jié)尾,而以換行符結(jié)尾。Python 使用硬回車來(lái)分割語(yǔ)句, 冒號(hào)和縮進(jìn)來(lái)分割代碼塊。C++ 和 Java 使用分號(hào)來(lái)分割語(yǔ)句, 花括號(hào)來(lái)分割代碼塊。 注釋 a. 使用#符號(hào)標(biāo)示注釋; b. 在模塊、類或者函數(shù)起始添加一個(gè)字符串起文檔作用; c. 使用三引號(hào)標(biāo)示注釋。 print """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """ 主流程 Python 中沒(méi)有子程序,只有函數(shù), 所有的函數(shù)都有返回值,并且所有的函數(shù)都以 def 開始。 字符串 Python中單引號(hào)與雙引號(hào)的區(qū)別類似于PHP中,雙引號(hào)中可以包括單引號(hào)。 數(shù)組 Python中數(shù)組下標(biāo)可以為負(fù)數(shù),即從右端開始計(jì)量,-1即為最后一個(gè)數(shù)。Python不可以修改數(shù)組中值,字符串下標(biāo)索引方式類似于MATLAB。 函數(shù) Python的函數(shù)可以嵌套定義
Installation:環(huán)境搭建
Conda
筆者推薦使用Anaconda作為環(huán)境搭建工具,并且推薦使用Python 3.5版本,可以在這里下載。如果是習(xí)慣使用Docker的小伙伴可以參考anaconda-notebook
- docker pull rothnic/anaconda-notebook
- docker run -p 8888:8888 -i -t rothnic/anaconda-notebook
安裝完畢之后可以使用如下命令驗(yàn)證安裝是否完畢:
- conda --version
安裝完畢之后我們就可以創(chuàng)建具體的開發(fā)環(huán)境了,主要是通過(guò)create命令來(lái)創(chuàng)建新的獨(dú)立環(huán)境:
- conda create --name snowflakes biopython
該命令會(huì)創(chuàng)建一個(gè)名為snowflakes并且安裝了Biopython的環(huán)境,如果你需要切換到該開發(fā)環(huán)境,可以使用activate命令:
- Linux, OS X: source activate snowflakes
- Windows: activate snowflakes
我們也可以在創(chuàng)建環(huán)境的時(shí)候指明是用python2還是python3:
- conda create --name bunnies python=3 astroid babel
環(huán)境創(chuàng)建完畢之后,我們可以使用info命令查看所有環(huán)境:
- conda info --envs
- conda environments:
- snowflakes * /home/username/miniconda/envs/snowflakes
- bunnies /home/username/miniconda/envs/bunnies
- root /home/username/miniconda
當(dāng)我們切換到某個(gè)具體的環(huán)境后,可以安裝依賴包了:
- conda list # 列舉當(dāng)前環(huán)境中的所有依賴包
- conda install nltk # 安裝某個(gè)新的依賴
Jupyter Notebook
在Conda安裝之后,Jupyter Notebook是默認(rèn)安裝好的,直接在工作目錄下打開即可:
jupyter notebook
基礎(chǔ)數(shù)據(jù)類型
和其他主流語(yǔ)言一樣,Python為我們提供了包括integer、float、boolean、strings等在內(nèi)的很多基礎(chǔ)類型。
數(shù)值類型
- x = 3
- print type(x) # Prints "<type 'int'>"
- print x # Prints "3"
- print x + 1 # Addition; prints "4"
- print x - 1 # Subtraction; prints "2"
- print x * 2 # Multiplication; prints "6"
- print x ** 2 # Exponentiation; prints "9"
- x += 1
- print x # Prints "4"
- x *= 2
- print x # Prints "8"
- y = 2.5
- print type(y) # Prints "<type 'float'>"
- print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
不過(guò)需要注意的是,Python并沒(méi)有x++或者x--這樣的自增或者自減操作符。另外,Python內(nèi)置的也提供了長(zhǎng)整型與其他復(fù)雜數(shù)值類型的整合,可以參考這里。
布爾類型
Python提供了常見(jiàn)的邏輯操作符,不過(guò)需要注意的是Python中并沒(méi)有使用&&、||等,而是直接使用了英文單詞。
- t = True
- f = False
- print type(t) # Prints "<type 'bool'>"
- print t and f # Logical AND; prints "False"
- print t or f # Logical OR; prints "True"
- print not t # Logical NOT; prints "False"
- print t != f # Logical XOR; prints "True"
字符串
Python對(duì)于字符串的支持還是很好的,不過(guò)需要注意到utf-8編碼問(wèn)題。
- hello = 'hello' # String literals can use single quotes
- world = "world" # or double quotes; it does not matter.
- print hello # Prints "hello"
- print len(hello) # String length; prints "5"
- hw = hello + ' ' + world # String concatenation
- print hw # prints "hello world"
- hw12 = '%s %s %d' % (hello, world, 12) # sprintf style string formatting
- print hw12 # prints "hello world 12"
Python中的字符串對(duì)象還包含了很多有用的方法,譬如:
- s = "hello"
- print s.capitalize() # Capitalize a string; prints "Hello"
- print s.upper() # Convert a string to uppercase; prints "HELLO"
- print s.rjust(7) # Right-justify a string, padding with spaces; prints " hello"
- print s.center(7) # Center a string, padding with spaces; prints " hello "
- print s.replace('l', '(ell)') # Replace all instances of one substring with another;
- # prints "he(ell)(ell)o"
- print ' world '.strip() # Strip leading and trailing whitespace; prints "world"
可以在這里中查看詳細(xì)的方法列表。
復(fù)雜數(shù)據(jù)類型
列表
Python中的列表等價(jià)于數(shù)組,不過(guò)其能夠動(dòng)態(tài)擴(kuò)展并且能夠存放不同類型的數(shù)值。
- xs = [3, 1, 2] # Create a list
- print xs, xs[2] # Prints "[3, 1, 2] 2"
- print xs[-1] # Negative indices count from the end of the list; prints "2"
- xs[2] = 'foo' # Lists can contain elements of different types
- print xs # Prints "[3, 1, 'foo']"
- xs.append('bar') # Add a new element to the end of the list
- print xs # Prints "[3, 1, 'foo', 'bar']"
- x = xs.pop() # Remove and return the last element of the list
- print x, xs # Prints "bar [3, 1, 'foo']"
同樣你可以在文檔中查看更多的細(xì)節(jié)。
切片
Python中對(duì)于數(shù)組的訪問(wèn)也相當(dāng)人性化,通過(guò)簡(jiǎn)單的操作符即可以完成對(duì)于數(shù)組中子數(shù)組的截取。
- nums = range(5) # range is a built-in function that creates a list of integers
- print nums # Prints "[0, 1, 2, 3, 4]"
- print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
- print nums[2:] # Get a slice from index 2 to the end; prints "[2, 3, 4]"
- print nums[:2] # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
- print nums[:] # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
- print nums[:-1] # Slice indices can be negative; prints ["0, 1, 2, 3]"
- nums[2:4] = [8, 9] # Assign a new sublist to a slice
- print nums # Prints "[0, 1, 8, 9, 4]"
遍歷
你可以使用基本的for循環(huán)來(lái)遍歷數(shù)組中的元素,就像下面介個(gè)樣紙:
- animals = ['cat', 'dog', 'monkey']
- for animal in animals:
- print animal
- # Prints "cat", "dog", "monkey", each on its own line.
如果你在循環(huán)的同時(shí)也希望能夠獲取到當(dāng)前元素下標(biāo),可以使用enumerate函數(shù):
- animals = ['cat', 'dog', 'monkey']
- for idx, animal in enumerate(animals):
- print '#%d: %s' % (idx + 1, animal)
- # Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line
變換
在編程中我們經(jīng)常需要對(duì)數(shù)組進(jìn)行變換,比較著名的我們可以使用map、reduce、filter這幾個(gè)函數(shù),而在Python中提供了非常方便的List Comprehension操作符。譬如我們需要對(duì)數(shù)組中元素進(jìn)行依次平方操作
- nums = [0, 1, 2, 3, 4]
- squares = []
- for x in nums:
- squares.append(x ** 2)
- print squares # Prints [0, 1, 4, 9, 16]
我們可以簡(jiǎn)寫為如下方式:
- nums = [0, 1, 2, 3, 4]
- squares = [x ** 2 for x in nums]
- print squares # Prints [0, 1, 4, 9, 16]
List Comprehensions也支持進(jìn)行條件選擇:
- nums = [0, 1, 2, 3, 4]
- even_squares = [x ** 2 for x in nums if x % 2 == 0]
- print even_squares # Prints "[0, 4, 16]"
字典
Python中的字典類型即類似于Java中的Map或者JavaScript中的Object,也就是所謂的鍵值對(duì)類型,基本的使用方式為:
- d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data
- print d['cat'] # Get an entry from a dictionary; prints "cute"
- print 'cat' in d # Check if a dictionary has a given key; prints "True"
- d['fish'] = 'wet' # Set an entry in a dictionary
- print d['fish'] # Prints "wet"
- # print d['monkey'] # KeyError: 'monkey' not a key of d
- print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A"
- print d.get('fish', 'N/A') # Get an element with a default; prints "wet"
- del d['fish'] # Remove an element from a dictionary
- print d.get('fish', 'N/A') # "fish" is no longer a key; prints "N/A"
遍歷
對(duì)于字典的遍歷也非常簡(jiǎn)單:
- d = {'person': 2, 'cat': 4, 'spider': 8}
- for animal in d:
- legs = d[animal]
- print 'A %s has %d legs' % (animal, legs)
- # Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
如果你希望同時(shí)訪問(wèn)鍵和其對(duì)應(yīng)的值,可以使用iteritems方法:
- d = {'person': 2, 'cat': 4, 'spider': 8}
- for animal, legs in d.iteritems():
- print 'A %s has %d legs' % (animal, legs)
- # Prints "A person has 2 legs", "A spider has 8 legs", "A cat has 4 legs"
變換
- nums = [0, 1, 2, 3, 4]
- even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
- print even_num_to_square # Prints "{0: 0, 2: 4, 4: 16}"
Set
Set是一系列無(wú)序且唯一的元素的集合:
- animals = {'cat', 'dog'}
- print 'cat' in animals # Check if an element is in a set; prints "True"
- print 'fish' in animals # prints "False"
- animals.add('fish') # Add an element to a set
- print 'fish' in animals # Prints "True"
- print len(animals) # Number of elements in a set; prints "3"
- animals.add('cat') # Adding an element that is already in the set does nothing
- print len(animals) # Prints "3"
- animals.remove('cat') # Remove an element from a set
- print len(animals) # Prints "2"
遍歷
集合遍歷的語(yǔ)法和數(shù)組遍歷很類似,不過(guò)因?yàn)榧媳旧硎菬o(wú)序的,因此你不能夠依賴于遍歷的順序來(lái)預(yù)測(cè)集合中元素的順序:
- animals = {'cat', 'dog', 'fish'}
- for idx, animal in enumerate(animals):
- print '#%d: %s' % (idx + 1, animal)
- # Prints "#1: fish", "#2: dog", "#3: cat"
變換
- from math import sqrt
- nums = {int(sqrt(x)) for x in range(30)}
- print nums # Prints "set([0, 1, 2, 3, 4, 5])"
Tuples
Python中的Tuple指不可變的有序元素集合,Tuple很類似于列表,不過(guò)區(qū)別在于Tuple可以做字典中的鍵類型,而列表則不可以。
- d = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keys
- t = (5, 6) # Create a tuple
- print type(t) # Prints "<type 'tuple'>"
- print d[t] # Prints "5"
- print d[(1, 2)] # Prints "1"
Function:函數(shù)
Python中的函數(shù)使用def關(guān)鍵字進(jìn)行定義,譬如:
- def sign(x):
- if x > 0:
- return 'positive'
- elif x < 0:
- return 'negative'
- else:
- return 'zero'
- for x in [-1, 0, 1]:
- print sign(x)
- # Prints "negative", "zero", "positive"
同時(shí),Python中的函數(shù)還支持可選參數(shù):
- def hello(name, loud=False):
- if loud:
- print 'HELLO, %s!' % name.upper()
- else:
- print 'Hello, %s' % name
- hello('Bob') # Prints "Hello, Bob"
- hello('Fred', loud=True) # Prints "HELLO, FRED!"
Classes:類
Python中對(duì)于類的定義也很直接:
- class Greeter(object):
- # Constructor
- def __init__(self, name):
- self.name = name # Create an instance variable
- # Instance method
- def greet(self, loud=False):
- if loud:
- print 'HELLO, %s!' % self.name.upper()
- else:
- print 'Hello, %s' % self.name
- g = Greeter('Fred') # Construct an instance of the Greeter class
- g.greet() # Call an instance method; prints "Hello, Fred"
- g.greet(loud=True) # Call an instance method; prints "HELLO, FRED!"
【本文是51CTO專欄作者“張梓雄 ”的原創(chuàng)文章,如需轉(zhuǎn)載請(qǐng)通過(guò)51CTO與作者聯(lián)系】