python内建函数是什么意思_python常用内建函数

内建函数是python解释器内置的函数,由cpython执行的c语言编写的函数,在加载速度上优于开发者自定义的函数,上一篇将python常用内建属性说了《python常用内建属性大全》,本篇说常用的内建函数。

当打开python解释器后输入dir(__builtins__)即可列举出python所有的内建函数:

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'Blocki

ngIOError', '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 reduce

reduce(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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值