python內置函數

Build-in Function,啓動python解釋器,輸⼊ dir(builtin) , 可以看到很
多python解釋器啓動後默認加載的屬性和函數,這些函數稱之爲內建函數,
這些函數因爲在編程時使⽤較多,cpython解釋器⽤c語⾔實現了這些函數,
啓動解釋器 時默認加載。
In [1]: dir(builtin)
Out[1]:
[‘ArithmeticError’,
‘AssertionError’,
‘AttributeError’,
‘BaseException’,
‘BufferError’,
‘BytesWarning’,
‘DeprecationWarning’,
‘EOFError’,
‘Ellipsis’,
‘EnvironmentError’,
‘Exception’,
‘False’,
‘FloatingPointError’,
‘FutureWarning’,
‘GeneratorExit’,
‘IOError’,
‘ImportError’,
‘ImportWarning’,
‘IndentationError’,
‘IndexError’,
‘KeyError’,
‘KeyboardInterrupt’,
‘LookupError’,
‘MemoryError’,
‘NameError’,
‘None’,
‘NotImplemented’,
‘NotImplementedError’,
‘OSError’,
‘OverflowError’,
‘PendingDeprecationWarning’,
‘ReferenceError’,
‘RuntimeError’,
‘RuntimeWarning’,
‘StandardError’,
‘StopIteration’,
‘SyntaxError’,
‘SyntaxWarning’,
‘SystemError’,
‘SystemExit’,
‘TabError’,
‘True’,
‘TypeError’,
‘UnboundLocalError’,
‘UnicodeDecodeError’,
‘UnicodeEncodeError’,
‘UnicodeError’,
‘UnicodeTranslateError’,
‘UnicodeWarning’,
‘UserWarning’,
‘ValueError’,
‘Warning’,
‘ZeroDivisionError’,
IPYTHON‘,
‘__IPYTHON__active’,
debug‘,
doc‘,
import‘,
name‘,
package‘,
‘abs’,
‘all’,
‘any’,
‘apply’,
‘basestring’,
‘bin’,
‘bool’,
‘buffer’,
‘bytearray’,
‘bytes’,
‘callable’,
‘chr’,
‘classmethod’,
‘cmp’,
‘coerce’,
‘compile’,
‘complex’,
‘copyright’,
‘credits’,
‘delattr’,
‘dict’,
‘dir’,
‘divmod’,
‘dreload’,
‘enumerate’,
‘eval’,
‘execfile’,
‘file’,
‘filter’,
‘float’,
‘format’,
‘frozenset’,
‘get_ipython’,
‘getattr’,
‘globals’,
‘hasattr’,
‘hash’,
‘help’,
‘hex’,
‘id’,
‘input’,
‘int’,
‘intern’,
‘isinstance’,
‘issubclass’,
‘iter’,
‘len’,
‘license’,
‘list’,
‘locals’,
‘long’,
‘map’,
‘max’,
‘memoryview’,
‘min’,
‘next’,
‘object’,
‘oct’,
‘open’,
‘ord’,
‘pow’,
‘print’,
‘property’,
‘range’,
‘raw_input’,
‘reduce’,
‘reload’,
‘repr’,
‘reversed’,
‘round’,
‘set’,
‘setattr’,
‘slice’,
‘sorted’,
‘staticmethod’,
‘str’,
‘sum’,
‘super’,
‘tuple’,
‘type’,
‘unichr’,
‘unicode’,
‘vars’,
‘xrange’,
‘zip’,
但是常用的內建函數不多,先理解常用的,遇到不常用的直接查資料即可
1 range函數的用法
在python解釋器中輸入help(range)即可查看range的用法
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)
range在python2和python3中用法不一樣,在python2中直接返回列表如
In [1]: range(5)
Out[1]: [0, 1, 2, 3, 4]在python3中
In [1]: range(5)
Out[1]: range(0, 5)
用list列表接收即可返回列表
但是一般用配合列表生成式來生成需要的列表
In [21]: li = [x+2 for x in range(5)]
In [22]: li
Out[22]: [2, 3, 4, 5, 6]

2 map函數的用法
map函數會根據提供的函數對指定序列做映射
map(…)
map(function, sequence[, sequence, …]) -> list
function:是⼀個函數
sequence:是⼀個或多個序列,取決於function需要⼏個參數
返回值是⼀個list(python2中返回list,python中返回map對象用戶list()接收即可)
python2中的實例
In [1]: map(lambda x:x*x,[1,2,2])
Out[1]: [1, 4, 4]
In [2]: map(lambda x,y:x+y,[1,2,2],[1,2,3])
Out[2]: [2, 4, 5]
總結:可以對兩個序列進行一些運算。

3 filter函數的用法
filter函數會對指定序列執⾏過濾操作
filter(…)
filter(function or None, sequence) -> list, tuple, or string
function:接受⼀個參數,返回布爾值True或False
sequence:序列可以是str,tuple,list
filter函數會對序列參數sequence中的每個元素調⽤function函數,最後返回
的結果包含調⽤結果爲True的元素。
返回值的類型和參數sequence的類型相同
In [3]: filter(lambda x:x/2, [1,2,3,4])
Out[3]: [2, 3, 4]
In [6]: filter(None,(1,2,3))
Out[6]: (1, 2, 3)
總結:可以過濾一些序列中不需要的元素

4 reduce函數的用法
reduce函數,reduce函數會對參數序列中元素進⾏累積
reduce(…)
reduce(function, sequence[, initial]) -> value
function:該函數有兩個參數
sequence:序列可以是str,tuple,list
initial:固定初始值
In [8]: reduce(lambda x,y:x+y,(1,2,3))
Out[8]: 6
In [10]: reduce(lambda x,y:x+y,(1,2,3,4,5,6),2)
Out[10]: 23
總結:可以很方便的對序列中的元素進行累加
這就是4種內置函數的基本用法,可以很方便的對序列進行一些操作,已到達目的,如有錯誤請及時指正,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章