104 python高级 - 内建函数
Build-in Function,启动python解释器,输入dir(builtins), 可以看到很多python解释器启动后默认加载的属性和函数,这些函数称之为内建函数, 这些函数因为在编程时使用较多,cpython解释器用c语言实现了这些函数,启动解释器 时默认加载。
这些函数数量众多,不宜记忆,开发时不是都用到的,待用到时再help(function), 查看如何使用,或结合百度查询即可,在这里介绍些常用的内建函数。
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)
python2中range返回列表,python3中range返回一个迭代值。如果想得到列表,可通过list函数.
a = range(5)
list(a)
创建列表的另外一种方法
In [21]: testList = [x+2 for x in range(5)]
In [22]: testList
Out[22]: [2, 3, 4, 5, 6]
map函数
map函数会根据提供的函数对指定序列做映射
map(...)
map(function, sequence[, sequence, ...]) -> list
- function:是一个函数
- sequence:是一个或多个序列,取决于function需要几个参数
- 返回值是一个list
参数序列中的每一个元素分别调用function函数,返回包含每次function函数返回值的list。
#函数需要一个参数
map(lambda x: x*x, [1, 2, 3])
#结果为:[1, 4, 9]
#函数需要两个参数
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' ]
l3 = map( f1, l1, l2 )
print(list(l3))
#结果为:[(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的元素。
返回值的类型和参数sequence的类型相同
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。
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'
在Python3里,reduce函数已经被从全局名字空间里移除了, 它现在被放置在fucntools模块里用的话要先引入: from functools import reduce
sorted函数
sorted(...)
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
相关文章
- 【Python】python 多线程两种实现方式
- 分析Python中解析构建数据知识
- Python 日期和时间_python 当前日期时间_python日期格式化
- Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接
- Python中常见的异常总结
- python处理多线程之间事件通讯方法
- [Python] Handle Exceptions to prevent crashes in Python
- 武功秘籍python实现
- 浅析算法的时间复杂度和空间复杂度 (C++/python双语实例)
- 华为OD机试 - 单词接龙(Java & JS & Python)
- Py之xlwt:python库之xlwt的简介、安装、使用方法之详细攻略
- Python之matplotlib:基于matplotlib库利用python语言实现一张画布显示多张图的多种方法
- Python语言学习:在python中,如何获取变量的本身字符串名字而非其值/内容及其应用(在代码中如何查找同值的所有变量名)
- Python:利用python语言绘制多个子图经典案例、代码实现之详细攻略
- Python语言学习:基于python五种方法实现使用某函数名【func_01】的字符串格式('func_01')来调用该函数【func_01】执行功能
- python --> Python初阶 --> 基础语法 --> 条件和分支
- 100天精通Python(数据分析篇)——第73天:Pandas文本数据处理方法之查找、替换、拼接、正则、虚拟变量
- Python每日一练——第19天:循环语句
- 已解决2.Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and wi
- 已解决2. Set PROTOCOL_BUPFERS_PYTHON_iMPLEMENTATION=python (but this will use pure-Python parsing and w
- python爬虫接私活赚了4万多
- 大神开车的标题-python中类方法、类实例方法、静态方法的使用与区别
- Python 实操案例:一文详解10种聚类算法
- 《Python智能优化算法:从原理到代码实现与应用》出版啦
- 【华为机试真题 Python实现】小兔子繁殖
- python prophet 预言家预知未来
- 9.4 marshal--Python内部对象序列化
- 在 Python 中使用定时器
- python基础===进程,线程,协程的区别(转)
- Python .py 文件打包成 .exe 文件(Windows平台,python 3.x)