In [33]:
a = list('hello') # a指向一個list物件
b = a # b指向a所指向的list物件
b[3] = 'x' # 改變物件第3個元素,因為實際件只有一個,所以a,b看到的物件會同時改變
a, b
Out[33]:
In [36]:
a = ['maybe']
b = [a, a, a]
b
Out[36]:
In [37]:
a[0] = 'will'
b
Out[37]:
注意: 如果你要複製list,必須用[:]來複製,否則只會複製指標。
In [45]:
a = ['play']
b = a[:]
a[0] = 'zero'
a, b
Out[45]:
In [48]:
a = ['play']
b = [a, a]
a[0] = 'what'
a, b, id(a), id(b[0])
Out[48]:
In [52]:
a is b[0], a is b[1]
Out[52]:
In [54]:
b = a[:]
# 因為用複製的,所以值相同但物件不同
a is b, a == b
Out[54]:
In [55]:
e = []
if e: print e, " is not empty"
In [62]:
e = []
if not e: print e, " is empty"
any()判斷一個list是否存在True的元素,all()判斷一個list是否全為True,in用來判斷值是否存在list中。
In [65]:
a = [0, 1, 2, 3, 4, 5]
any(a), all(a), 3 in a, 8 in a
Out[65]:
In [75]:
a = [3, 3, 2, 4, 1]
[item for item in a] # 原始順序
Out[75]:
In [76]:
[item for item in sorted(a)] # 排序
Out[76]:
In [77]:
[item for item in set(a)] # 只考慮唯一的元素
Out[77]:
In [78]:
[item for item in reversed(a)] # 倒序
Out[78]:
In [79]:
[item for item in set(a).difference([3,4])] # 不要某些元素
Out[79]:
In [88]:
import random
random.shuffle(a) # shuffle後,會直接影響a內部的值
[item for item in a]
Out[88]:
In [90]:
''.join(['hello', 'world']) # join可以將字串連在一起
Out[90]:
利用tuple可以同時進行多個元素的取代。
In [93]:
a = [1, 2, 3, 4, 5]
(a[2], a[3], a[4]) = (5, 6, 7)
a
Out[93]:
用zip可以將多個list結合成tuple。
In [97]:
a = range(5)
b = range(5, 10)
zip(a, b, a, b)
Out[97]:
In [101]:
list(enumerate(b)) # enumerate 會傳回 (index, a[index])
Out[101]:
In [105]:
a = [5, 3, 2, 4, 1]
a.sort() # .sort() 會直接修改原始list
a
Out[105]:
In [108]:
a = [5, 3, 2, 4, 1]
sorted(a), a # 用sorted()不會影響原始list
Out[108]:
In [109]:
'hello' * 3
Out[109]:
In [110]:
['hello'] * 3
Out[110]:
In [111]:
[['a'] * 3] * 2
Out[111]:
在設計function時,要注意,如果會修改輸入參數,最好不要有輸出,否則會讓使用者混淆。
def sort1(a): # OK, 會修改輸入但沒有輸出
a.sort()
def sort2(a): # OK, 不會修改輸入, 有輸出
return sorted(a)
def sort3(a): # BAD, 有修改輸入又有輸出, 一定會有人搞錯
a.sort()
return a
所有function的參數都是call-by-value,但要注意,如果參數是一個list,list傳入的value是物件id,傳到function內部後變成可修改的list。
In [116]:
def func1(a):
a[0] = 'modified'
s = ['hello', 'world']
func1(s)
s
Out[116]:
In [122]:
a = 'hello'
assert(isinstance(a, basestring)) # 沒問題
In [123]:
a = 3
assert(isinstance(a, basestring)) # 錯誤
In [124]:
def hello(a):
"""
This is a hello function.
The only function is print hello world.
@param a: a string to be printed
@type a: C{basestring}
@rtype: C{float}
"""
print 'hello world', a
return(3.14)
hello('my dear')
Out[124]:
In [126]:
print hello.__doc__
In [129]:
z = lambda w: w**2
z(5)
Out[129]:
In [131]:
def generic(*a, **b):
print a # 集中所有 unnamed arguments
print b # 集中所有 names arguments
generic(1, 3.5, 'money', zzz='maybe', ggg='good')
In [134]:
def func(*a, z):
print a, z # 因為有指定 *a 收集所有 unnamed arguments,造成 z 出錯
func('hi', 'this')
In [137]:
nltk.corpus.__file__
Out[137]:
In [143]:
help(nltk.bigrams)
In [146]:
def insert(trie, key, value):
if key:
first, rest = key[0], key[1:]
if first not in trie:
trie[first] = {} # empty dict
insert(trie[first], rest, value) # key[1:] is new key
else:
trie['value'] = value
trie = nltk.defaultdict(dict)
In [151]:
insert(trie, 'chat', 100)
insert(trie, 'chair', 2000)
insert(trie, 'chien', 150)
trie
Out[151]:
In [153]:
trie['c']['h']['a']['t']['value']
Out[153]:
In [154]:
%matplotlib inline
import matplotlib
In [157]:
from matplotlib import pylab
In [1]:
import nltk
In [ ]:
nltk.ngrams()