python常用内建函数
內建函數是python解釋器內置的函數,由cpython執行的c語言編寫的函數,在加載速度上優于開發者自定義的函數,上一篇將python常用內建屬性說了《python常用內建屬性大全》,本篇說常用的內建函數。
當打開python解釋器后輸入dir(__builtins__)即可列舉出python所有的內建函數:
['ArithmeticError',?'AssertionError',?'AttributeError',?'BaseException',?'BlockingIOError',?'BrokenPipeError',?'BufferError',?'BytesWarning',?'ChildProcessError
',?'ConnectionAbortedError',?'ConnectionError',?'ConnectionRefusedError',?'Conne
ctionResetError',?'DeprecationWarning',?'EOFError',?'Ellipsis',?'EnvironmentErro
r',?'Exception',?'False',?'FileExistsError',?'FileNotFoundError',?'FloatingPoint
Error',?'FutureWarning',?'GeneratorExit',?'IOError',?'ImportError',?'ImportWarni
ng',?'IndentationError',?'IndexError',?'InterruptedError',?'IsADirectoryError',
'KeyError',?'KeyboardInterrupt',?'LookupError',?'MemoryError',?'ModuleNotFoundEr
ror',?'NameError',?'None',?'NotADirectoryError',?'NotImplemented',?'NotImplement
edError',?'OSError',?'OverflowError',?'PendingDeprecationWarning',?'PermissionEr
ror',?'ProcessLookupError',?'RecursionError',?'ReferenceError',?'ResourceWarning
',?'RuntimeError',?'RuntimeWarning',?'StopAsyncIteration',?'StopIteration',?'Syn
taxError',?'SyntaxWarning',?'SystemError',?'SystemExit',?'TabError',?'TimeoutErr
or',?'True',?'TypeError',?'UnboundLocalError',?'UnicodeDecodeError',?'UnicodeEnc
odeError',?'UnicodeError',?'UnicodeTranslateError',?'UnicodeWarning',?'UserWarni
ng',?'ValueError',?'Warning',?'WindowsError',?'ZeroDivisionError',?'__build_clas
s__',?'__debug__',?'__doc__',?'__import__',?'__loader__',?'__name__',?'__package
__',?'__spec__',?'abs',?'all',?'any',?'ascii',?'bin',?'bool',?'breakpoint',?'byt
earray',?'bytes',?'callable',?'chr',?'classmethod',?'compile',?'complex',?'copyr
ight',?'credits',?'delattr',?'dict',?'dir',?'divmod',?'enumerate',?'eval',?'exec
',?'exit',?'filter',?'float',?'format',?'frozenset',?'getattr',?'globals',?'hasa
ttr',?'hash',?'help',?'hex',?'id',?'input',?'int',?'isinstance',?'issubclass',?'
iter',?'len',?'license',?'list',?'locals',?'map',?'max',?'memoryview',?'min',?'n
ext',?'object',?'oct',?'open',?'ord',?'pow',?'print',?'property',?'quit',?'range
',?'repr',?'reversed',?'round',?'set',?'setattr',?'slice',?'sorted',?'staticmeth
od',?'str',?'sum',?'super',?'tuple',?'type',?'vars',?'zip']
map函數
map函數對指定序列映射到指定函數
map(function, sequence[, sequence, ...]) -> list對序列中每個元素調用function函數,返回列表結果集的map對象
#函數需要一個參數list(map(lambda?x:?x*x,?[1,?2,?3]))
#結果為:[1,?4,?9]
#函數需要兩個參數
list(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'?]
a?=?map(?f1,?l1,?l2?)?
print(list(a))
#結果為:[(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的filter對象。
與map不同的是map是返回return的結果,而filter是更具return True條件返回傳入的參數,一個是加工一個是過濾。
返回值的類型和參數sequence的類型相同
list(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。
空間里移除了,?它現在被放置在fucntools模塊里用的話要先引入:?from?functools?import?reducereduce(lambda?x,?y:?x+y,?[1,2,3,4])
10
reduce(lambda?x,?y:?x+y,?[1,2,3,4],?5)
15
reduce(lambda?x,?y:?x+y,?['aa',?'bb',?'cc'],?'dd')
'ddaabbcc'
總結
以上是生活随笔為你收集整理的python常用内建函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新手向:从不同的角度来详细分析Redis
- 下一篇: No sleep, no sex, no