python常用内建函数
內(nèi)建函數(shù)是python解釋器內(nèi)置的函數(shù),由cpython執(zhí)行的c語言編寫的函數(shù),在加載速度上優(yōu)于開發(fā)者自定義的函數(shù),上一篇將python常用內(nèi)建屬性說了《python常用內(nèi)建屬性大全》,本篇說常用的內(nèi)建函數(shù)。
當(dāng)打開python解釋器后輸入dir(__builtins__)即可列舉出python所有的內(nèi)建函數(shù):
['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函數(shù)
map函數(shù)對(duì)指定序列映射到指定函數(shù)
map(function, sequence[, sequence, ...]) -> list對(duì)序列中每個(gè)元素調(diào)用function函數(shù),返回列表結(jié)果集的map對(duì)象
#函數(shù)需要一個(gè)參數(shù)list(map(lambda?x:?x*x,?[1,?2,?3]))
#結(jié)果為:[1,?4,?9]
#函數(shù)需要兩個(gè)參數(shù)
list(map(lambda?x,?y:?x+y,?[1,?2,?3],?[4,?5,?6]))
#結(jié)果為:[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))
#結(jié)果為:[(0,?'Sun'),?(1,?'M'),?(2,?'T'),?(3,?'W'),?(4,?'T'),?(5,?'F'),?(6,?'S')]
filter函數(shù)
filter函數(shù)會(huì)對(duì)指定序列按照規(guī)則執(zhí)行過濾操作
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:接受一個(gè)參數(shù),返回布爾值True或False
-
sequence:序列可以是str,tuple,list
filter函數(shù)會(huì)對(duì)序列參數(shù)sequence中的每個(gè)元素調(diào)用function函數(shù),最后返回的結(jié)果包含調(diào)用結(jié)果為True的filter對(duì)象。
與map不同的是map是返回return的結(jié)果,而filter是更具return True條件返回傳入的參數(shù),一個(gè)是加工一個(gè)是過濾。
返回值的類型和參數(shù)sequence的類型相同
list(filter(lambda?x:?x%2,?[1,?2,?3,?4]))[1,?3]
filter(None,?"she")
'she'
reduce函數(shù)
reduce函數(shù),reduce函數(shù)會(huì)對(duì)參數(shù)序列中元素進(jìn)行累積
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:該函數(shù)有兩個(gè)參數(shù)
-
sequence:序列可以是str,tuple,list
-
initial:固定初始值
reduce依次從sequence中取一個(gè)元素,和上一次調(diào)用function的結(jié)果做參數(shù)再次調(diào)用function。 第一次調(diào)用function時(shí),如果提供initial參數(shù),會(huì)以sequence中的第一個(gè)元素和initial 作為參數(shù)調(diào)用function,否則會(huì)以序列sequence中的前兩個(gè)元素做參數(shù)調(diào)用function。 注意function函數(shù)不能為None。
空間里移除了,?它現(xiàn)在被放置在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'
總結(jié)
以上是生活随笔為你收集整理的python常用内建函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新手向:从不同的角度来详细分析Redis
- 下一篇: No sleep, no sex, no