Python中内建函数的简单用法说明
Python提供了一个内联模块buildin,该模块定义了一些软件开发中经常用到的函数,利用这些函数可以实现数据类型的转换、数据的计算、序列的处理等。
buildin模块的内置函数:
1、apply():可以调用可变参数列表的函数,把参数存在一个元组或者序列中,apply元组参数必须和sum()的参数一致
#!/usr/bin/python # -*- coding:utf8 -*- def sum(x=1,y=2): return x+y print apply(sum,(1,3))
2、filter():可以对某个序列进行过滤,其中过滤的func()参数不能为空.
filter(func or None,sequence) –>list,tuple,or string
#!/usr/bin/python # -*- coding:utf8 -*- def func(x): if x>0: return x print filter(func,range(-9,10))
3、reduce() :对序列中的元素连续操作可以通过循环来处理,具有连续处理的功能。
reduce(func,sequence[,initial]) –> value
func是自定义函数,func()中实现对参数sequence的连续操作,sequence为待处理序列,如果参数
initial的值不为空,将首先传入函数func()进行计算,如果为空,则对initial的值进行处理
#!/usr/bin/python # -*- coding:utf8 -*- def sum(x,y): return x + y print reduce(sum,range(0,10)) print range(0,10) print reduce(sum,range(0,10),10) print reduce(sum,range(0,2),10)
4、map():可以对多个序列的每个元素都执行相同的操作,并组成列表返回。
如果提供多个序列,则每个序列中的元素一一对应进行计算;如果每个序列的长度不相同,
则短的序列后补充“None”,再进行计算
map(func,sequence[,sequence,…]) –> list #!/usr/bin/python # -*- coding:utf8 -*- def power(x): return x**x print map(power,range(1,5)) def power2(x,y): return x**y print map(power2,range(1,5),range(5,1,-1)) print range(1,5) print range(5,1,-1)
PS:常用内置模块函数:
abs(x) 返回x的绝对值
apply(func[,args[,kwargs]]) 把函数的参数放置在序列中传入函数
bool([x]) 把每一个值或者表达式转换为bool类型,如果表达式x为值,则返回True,否则返回False
cmp(x,y) 比较x,y的大小
delattr(obj,name) 等价于del obj.name
eval(s[,globals[,locals]]) 计算表达式的值
float(x) 把数字或者字符串转换成float类型数据
hash(object) 返回一个对象的hash值
help([object]) 返回内联函数的帮助说明
id(x) 返回一个对象的标示
input([prompt]) 接受控制台的输入,并把输入的值转换成数字
int(x) 把数字或字符串转换为整型
len(obj) 对象包含的元素的个数
range([start,]end[,step]) 生产一个列表并返回
raw_input([prompt]) 接受控制台的输入,返回字符串类型
reduce(func,sequence[,initial]) 对序列的值进行累加计算
round(x,n=0) 四舍五入的函数
set([interable]) 返回一个set集合
sorted(iterable[,cmp[,key[,reverse]]]) 返回一个排序后的列表
sum(iterable[,start=0]) 返回一个序列的和
type(obj) 返回一个对象的类型
xrange(start[,end[,step]]) 功能和range()类似,但是一次返回一个值
zip(seq1[,seq2,…]) 把n个序列作为列表的元素返回

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...
