内置函数总结

一.数字相关

1.绝对值:abs(-1)

2.最大最小值:max([1,2,3]) ,min([1,2,3])

3.序列长度:len('abc')  ,  len([1,2,3])  ,  len((1,2,3))

4.取模:divmod(5,2)//(2,1)

5.乘方:pow(2,3,4)//2**3/4

6.浮点数:round(1)//1.0

二.功能相关

1.函数是否可调用:callable(funcname),注意,funcname变量要定义过

2.类型判断:isinstance(x,list/int)

3.比较:cmp('hello','hello')

4.快速生成序列:(x)range([start,]stop[,step])

三.类型转换

1.int(x)

2.long(x)

3.float(x)

4.complex(x)     #复数

5.str(x)

6.list(x)

7.tuple(x)     #元组

8.hex(x)

9.oct(x)

10.chr(x)      #返回x对应的字符。如chr(65)返回‘A’

11.ord(x)      #返回字符对应的ASC数字编号,如ord('A')返回65

四.字符串处理

1.首字母大写:str.capitazlize

'hello'.capitalize()
 View Code

2.字符串替换:str.replace

'hello'.replace('l','2')
 View Code

3.字符串切割:str.split

 'hello'.split('l')
 View Code

可以传两个参数,第二个参数为切割次数。

以上三个方法都可以引用String模块,然后用string.xxx的方式进行调用

五.序列处理函数

1.len:序列长度

2.max:序列中最大值

3.min:序列中最小值

4.filter:过滤序列

filter(lambda x:x%2==0, [1,2,3,4,5,6])
 结果如下:
[2, 4, 6]

5.zip:并行遍历

>>> name=['jim','tom','lili']
 >>> age=[20,30,40]
 >>> tel=['133','156','189']
 >>> zip(name,age,tel)

 [('jim', 20, '133'), ('tom', 30, '156'), ('lili', 40, '189')]

注意,如果序列长度不同时,会出现下面的结果:

>>> name=['jim','tom','lili']
 >>> age=[20,30,40]
 >>> tel=['133','170']
 >>> zip(name,age,tel)
 [('jim', 20, '133'), ('tom', 30, '170')]

6.map:并行遍历,可接受一个function类型的参数

a=[1,3,5]
b=[2,4,6]
map(None,a,b)
[(1,2),(3,4),(5,6)]
map(lambda x,y : x * y,a,b)

[2,12,30]
原文地址:https://www.cnblogs.com/wanghaohao/p/7308597.html