python-内置函数

Python 内置函数

  内置函数  
abs() divmod() input() open() staticmethod()
all() enumerate() int() ord() str()
any() eval() isinstance() pow() sum()
basestring() execfile() issubclass() print() super()
bin() file() iter() property() tuple()
bool() filter() len() range() type()
bytearray() float() list() raw_input() unichr()
callable() format() locals() reduce() unicode()
chr() frozenset() long() reload() vars()
classmethod() getattr() map() repr() xrange()
cmp() globals() max() reversed() zip()
compile() hasattr() memoryview() round() __import__()
complex() hash() min() set()  
delattr() help() next() setattr()  
dict() hex() object() slice()  
dir() id() oct() sorted() exec 内置表达式

简单介绍如下:

1.abs() 求绝对值

2.all() 如果 iterable 的所有元素都为真(或者如果可迭代为空),则返回 True

3.any() 如果 iterable 的任何元素为真,则返回 True。如果iterable为空,则返回 False

4.callable() 如果 object 参数出现可调,则返回 True,否则返回 False

5.divmod() 以两个(非复数)数字作为参数,并在使用整数除法时返回由商和余数组成的一对数字。对于混合操作数类型,二进制算术运算符的规则适用。对于整数,结果与 (a//b,a%b) 相    同。对于浮点数,结果为 (q,a%b),其中q 通常为 math.floor(a/b),但可以小于1

6.enumerate() 参数必须是可迭代对象,函数运行结果得到一个迭代器,输出元素及对应的索引值

7.eval() 把字符串中的提取出来执行

8.frozenset() 不可变集合,frozenset()定义的集合不可增删元素

9.globals() 返回表示当前全局符号表的字典。这始终是当前模块的字典(在函数或方法内部,这是定义它的模块,而不是从其调用它的模块)

10.round() 对参数进行四舍五入

11.sorted() 排序,不改变原列表

l=[1,2,4,9,-1]
print(sorted(l)) #从小到大
print(sorted(l,reverse=True)) #从大到小

12.zip() 拉链函数 

创建一个迭代器,聚合来自每个迭代器的元素。

返回元组的迭代器,其中 i-th元组包含来自每个参数序列或迭代的第 i 个元素。当最短输入可迭代被耗尽时,迭代器停止。使用单个可迭代参数,它返回1元组的迭代器。没有参数,它返回一个空的迭代器

13.max() 

返回可迭代的最大项或两个或更多参数中最大的一个。

如果提供了一个位置参数,它应该是一个 iterable。返回迭代中的最大项。如果提供了两个或多个位置参数,则返回最大的位置参数。

max()可以指定key(也就是指定要比较的部分)

14.map() 映射

返回一个迭代器,它应用 function 到 iterable 的每个项目,产生结果

l=[1,2,3,4]
m=map(lambda x:x**2,l)
print(list(m))        ----->[1, 4, 9, 16]

15.reduce() 合并

from functools import reduce

res=0
for i in range(100):
    res+=i
print(res)

16.filter() 过滤  保留布尔值为True的元素

names=['alex_sb','yuanhao_sb','wupeiqi_sb','egon']
print(list(filter(lambda name:name.endswith('_sb'),names)))--->['alex_sb', 'yuanhao_sb', 'wupeiqi_sb']

详细的内置函数介绍可以参照以下:https://www.rddoc.com/doc/Python-3.6.0/library/functions/

原文地址:https://www.cnblogs.com/zhangningyang/p/7268654.html