內建函數
Build-in Function,啟動python解譯器,輸入dir(__builtins__), 可以看到很多python解譯器啟動後預設載入的屬性和函數,這些函數稱之為內建函數, 這些函數因為在編程時使用較多,cpython解譯器用c語言實現了這些函數,啟動解譯器 時預設載入。
這些函數數量眾多,不宜記憶,開發時不是都用到的,待用到時再help(function), 查看如何使用,或結合百度查詢即可,在這裡介紹些常用的內建函數。 range
range(stop) -> list of integers range(start, stop[, step]) -> list of integers
start:計數從start開始。預設是從0開始。例如range(5)等價於range(0, 5); stop:到stop結束,但不包括stop.例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5 step:每次跳躍的間距,預設為1。例如:range(0, 5) 等價於 range(0, 5, 1)
python2中range返回列表,python3中range返回一個迭代值。如果想得到列表,可通過list函數
a = range(5)list(a)
建立列表的另外一種方法
In [21]: testList = [x+2 for x in range(5)]In [22]: testListOut[22]: [2, 3, 4, 5, 6]
map函數
map函數會根據提供的函數對指定序列做映射
map(...) map(function, sequence[, sequence, ...]) -> list
function:是一個函數 sequence:是一個或多個序列,取決於function需要幾個參數
傳回值是一個list
參數序列中的每一個元素分別調用function函數,返回包含每次function函數傳回值的list。
#函數需要一個參數map(lambda x: x*x, [1, 2, 3])#結果為:[1, 4, 9]#函數需要兩個參數map(lambda x, y: x+y, [1, 2, 3], [4, 5, 6])#結果為:[5, 7, 9]def f1( x, y ): return (x,y)l1 = [ 0, 1, 2, 3, 4, 5, 6 ] l2 = [ 'Sun', 'M', 'T', 'W', 'T', 'F', 'S' ]l3 = map( f1, l1, l2 ) print(list(l3))#結果為:[(0, 'Sun'), (1, 'M'), (2, 'T'), (3, 'W'), (4, 'T'), (5, 'F'), (6, 'S')]
filter函數
filter函數會對指定序列執行過濾操作
filter(...) filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.
function:接受一個參數,返回布爾值True或False sequence:序列可以是str,tuple,list
filter函數會對序列參數sequence中的每個元素調用function函數,最後返回的結果包含調用結果為True的元素。
傳回值的類型和參數sequence的類型相同
filter(lambda x: x%2, [1, 2, 3, 4])[1, 3]filter(None, "she")'she'
reduce函數
reduce函數,reduce函數會對參數序列中元素進行累積
reduce(...) reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.
function:該函數有兩個參數 sequence:序列可以是str,tuple,list initial:固定初始值
reduce依次從sequence中取一個元素,和上一次調用function的結果做參數再次調用function。 第一次調用function時,如果提供initial參數,會以sequence中的第一個元素和initial 作為參數調用function,否則會以序列sequence中的前兩個元素做參數調用function。 注意function函數不能為None。
reduce(lambda x, y: x+y, [1,2,3,4])10reduce(lambda x, y: x+y, [1,2,3,4], 5)15reduce(lambda x, y: x+y, ['aa', 'bb', 'cc'], 'dd')'ddaabbcc'
在Python3裡,reduce函數已經被從全域名字空間裡移除了, 它現在被放置在fucntools模組裡用的話要先引入: from functools import reduce sorted函數
sorted(...) sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
In [1]: sorted([1, 4, 2, 6, 5, 3])Out[1]: [1, 2, 3, 4, 5, 6]In [2]: sorted([1, 4, 2, 6, 5, 3],reverse = 1)Out[2]: [6, 5, 4, 3, 2, 1]In [4]: sorted(['dd', 'aa', 'cc', 'bb'])Out[4]: ['aa', 'bb', 'cc', 'dd']In [5]: sorted(['dd', 'aa', 'cc', 'bb'], reverse = 1)Out[5]: ['dd', 'cc', 'bb', 'aa']