内建函数是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.  Iffunction is None, return the items that are true.  If sequence is a tupleor 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 itemsof the sequence in the calculation, and serves as a default when thesequence 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'

python常用内建函数相关推荐

  1. list python 访问 键值对_基础|Python常用知识点汇总(中)

    字符串字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串.1.创建字符串 str1 = 'Hello World!' str2 = "Hello W ...

  2. python list大小_4个python常用高阶函数的使用方法

    1.map Python内建了map()函数,map()函数接受两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每一个元素上,并把结果作为新的Iterator返回. 举 ...

  3. Python 的内建函数

    Python 的内建函数 Python 的内建函数 A类 B类 C类 D类 E类 F类 G类 H类 I类 L类 M类 N类 O类 P类 R类 S类 T类 V类 Z类 Python 的内建函数 A类 a ...

  4. Python常用6个技术网站汇总分享!

    Python是一门面向对象的编程语言,它具有丰富和强大的库,能够把用其他语言编写的各种模块轻松地联结在一起,因此也常被称为"胶水语言".Python技术会随着互联网的不断发展一直迭 ...

  5. GitHub上7000+ Star的Python常用代码合集

    作者 | 二胖并不胖 来源 | 大数据前沿(ID:bigdataqianyan) 今天二胖给大家介绍一个由一个国外小哥用好几年时间维护的Python代码合集.简单来说就是,这个程序员小哥在几年前开始保 ...

  6. python常用类库_Python常用库

    Python常用库 一.time:时间处理模块 import time 1.time.time() time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数). import tim ...

  7. 实战篇一 python常用模块和库介绍

    # -_-@ coding: utf-8 -_-@ -- Python 常用模块和库介绍 第一部分:json模块介绍 import json 将一个Python数据结构转换为JSON: dict_ = ...

  8. python常用函数-python常用函数精讲

    原标题:python常用函数精讲 返回值为bool类型的函数 bool是Boolean的缩写,只有真(True)和假(False)两种取值 bool函数只有一个参数,并根据这个参数的值返回真或者假. ...

  9. python常用模块大全总结-常用python模块

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. python常用模块什么是模块? 常见的场景:一个模块就是一个包含了py ...

最新文章

  1. PyTorch 笔记(11)— Tensor内部存储结构(头信息区 Tensor,存储区 Storage)
  2. 001_ECharts入门
  3. 四、pink老师的学习笔记——元素的显示与隐藏
  4. Go Elasticsearch 删除快速入门
  5. Halcon缺陷检测——机器学习1
  6. 运算符重载,输出流运算符重载
  7. 安装 Dynamics AX 2012 Data Migration Framework
  8. Debian 10 使用 rz sz 命令
  9. 2022年第十九届五一数学建模竞赛 C题 火灾报警系统问题
  10. debian修改键盘布局
  11. jwt怎么获取当前登录用户_获取jwt(json web token)中存储的用户信息
  12. OpenGL ES2.0 的三种变量类型(uniform,attribute和varying)
  13. 使用可道云在centos上搭建个人网盘(附带端口修改)
  14. spring源码阅读笔记09:循环依赖
  15. Windows下学习C语言有哪些集成开发软件?
  16. FISCO BCOS迎来开源智能合约编程语言Liquid
  17. 如何用天干地支计算年月日时?
  18. 4.10nbsp;经济周期和经济危机
  19. “笨办法”学Python3,Zed A. Shaw, 习题11
  20. Java数字签名校验

热门文章

  1. golang常见内存泄漏
  2. 建立索引要考虑的因素
  3. MySQL笔记9:内连接、左连接、右连接以及全连接查询
  4. 设计模式:模板方法(Template Method Pattern)
  5. [Java] SpringMVC工作原理之四:MultipartResolver
  6. 2018“硅谷技划”随笔(一):再论中美员工福利巨大差距的背后
  7. autoburn eMMC hacking
  8. 快速操作Linux终端命令行的快捷键…
  9. 生活大爆炸第6季第12集
  10. 如何制作网线标签和贴标签