Python函数之内置函数
内置函数清单如下:
详情可参考官方文档:https://docs.python.org/3/library/functions.html#next
或菜鸟教程中文文档:http://www.runoob.com/python/python-built-in-functions.html
下面简单介绍一下重要的内置函数,以加深对其的理解与记忆。
map() :根据提供的函数对指定序列做映射
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表
语法:
map(function, iterable, ...)
参数:
- function -- 函数,有两个参数
- iterable -- 一个或多个序列
返回值:py2返回列表;py3返回迭代器
实例:
from collections import Iterable,Iterator def square(x): return x ** 2 iter1 = map(square,[1, 2, 3, 4, 5]) print(isinstance(iter1,Iterator)) # True for i in iter1: print(i) # 采用匿名函数 iter2 = map(lambda x: x * x, [1, 2, 3, 4, 5]) print(isinstance(iter2,Iterator)) # True for i in iter2: print(i) # 提供多个可迭代对象 iter3 = map(lambda x, y: x * y, [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) print(isinstance(iter3,Iterator)) # True for i in iter3: print(i)
def square(x): return x ** 2 iter1 = map(square,[1, 2, 3, 4, 5]) print(iter1) # [1, 4, 9, 16, 25] # 采用匿名函数 iter2 = map(lambda x: x * x, [1, 2, 3, 4, 5]) print(iter2) # [1, 4, 9, 16, 25] # 提供多个可迭代对象 iter3 = map(lambda x, y: x * y, [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) print(iter3) # [1, 4, 9, 16, 25]
reduce(): 对参数序列中元素进行累积
该函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。
注意:在 Python3 中,reduce() 函数已经被从全局名字空间里移除了,在py3中被放置在 fucntools 模块里,如果想要使用它,则需要通过引入 functools 模块来调用 reduce() 函数。
语法:
reduce(function, iterable[, initializer])
参数:
- function -- 函数,有两个参数
- iterable -- 可迭代对象
- initializer -- 可选,初始参数
返回值:返回函数计算结果
实例:
from functools import reduce def add(x,y): return x + y print(reduce(add,[1,2,3,4,5])) # 15 print(reduce(add,[1,2,3,4,5],10)) # 25 # 采用匿名函数 print(reduce(lambda x,y:x+y,[1,2,3,4,5])) # 15
map()&reduce()的区别:
1. 从第一个参数(函数)接收参数分析:
map():可以接收一个或多个参数
reduce():必须接收两个参数
2. 从对传进去的数值作用来讲
map():将传入的函数依次作用到序列的每个元素,每个元素都是独自被函数“作用”一次,独自得到新的结果
reduce():将传人的函数首次作用在第1、2元素,后续依次作用在序列的每一个元素,最终结果是所有的元素相互作用的结果
range() :创建一个整数列表,一般用在 for 循环中
语法:
range(start, stop[, step])
参数:
- 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)
返回值:py2返回一个列表,py3返回一个整数序列的对象(可迭代)
实例:
print(range(5)) # range(0, 5) print(type(range(5))) # <class 'range'> # 利用 list 函数返回列表 print(list(range(5))) # [0, 1, 2, 3, 4] print(list(range(0,10,2))) # [0, 2, 4, 6, 8] # 用于for循环 for i in range(5): print(i)
print(range(5)) # [0, 1, 2, 3, 4] print(range(0,10,2)) # [0, 2, 4, 6, 8] # 用于for循环 for i in range(5): print(i
zip():用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
语法:
zip([iterable, ...])
参数:
- iterabl -- 一个或多个迭代器
返回值:Python 2.x 返回元组列表;Python 3.x 返回迭代器
实例:
from collections import Iterable,Iterator a = [1,2,3] b = [4,5,6,7,8] c = ['a','b','c'] zip1 = zip(a,c) print(isinstance(zip1,Iterator)) # True for i in zip1: print(i) zip2 = zip(b,c) print(isinstance(zip2,Iterator)) # True for i in zip2: print(i) print(isinstance(zip(*zip1),Iterator)) # True for i in zip(*zip1): # 因为zip1已被next()取完值,所以再进行for循环无值输出 print(i)
a = [1,2,3] b = [4,5,6,7,8] c = ['a','b','c'] zip1 = zip(a,c) print(zip1) # [(1, 'a'), (2, 'b'), (3, 'c')] zip2 = zip(b,c) print(zip2) # [(4, 'a'), (5, 'b'), (6, 'c')] print(zip(*zip1)) # [(1, 2, 3), ('a', 'b', 'c')] print(zip(*zip2)) # [(4, 5, 6), ('a', 'b', 'c')]
enumerate() :用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
语法:
enumerate(sequence, [start=0])
参数:
- sequence -- 一个序列、迭代器或其他支持迭代对象。
- start -- 下标起始位置。
返回值: enumerate(枚举) 对象
实例:
a = ['a','b','c'] print(enumerate(a)) # <enumerate object at 0x0000000002CEC1B0> print(list(enumerate(a))) # [(0, 'a'), (1, 'b'), (2, 'c')] print(list(enumerate(a,start=3))) # [(3, 'a'), (4, 'b'), (5, 'c')] # 用于for循环 for enu in enumerate(a): print(enu)
type():如果只有第一个参数则返回对象的类型,三个参数返回新的类型对象
语法:
class type(name, bases, dict)
参数:
- name -- 类的名称。
- bases -- 基类的元组。
- dict -- 字典,类内定义的命名空间变量。
返回值:一个参数返回对象类型, 三个参数,返回新的类型对象。
实例:
a = 1
b = []
c ={'name':'Joe'}
print(type(a)) # <type 'int'>
print(type(b)) # <type 'list'>
print(type(c)) # <type 'dict'>
class ClassName(object):
pass
cn = ClassName()
print(type(cn)) # <class '__main__.ClassName'>
print(type(ClassName)) # <type 'type'> 后面会详细介绍
# 三个参数
cn = type('ClassName',(object,),{'a':1})
print(cn) # <class '__main__.ClassName'>
print(type(ClassName)) # <type 'type'>
isinstance() :用来来判断一个对象是否是一个已知的类型,类似 type()。
语法:
isinstance(object, classinfo)
参数:
- object -- 实例对象。
- classinfo -- 可以是直接或间接类名、基本类型或者由它们组成的元组。
返回值:如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False
实例:
a = 1 b = [] class ClassName(object): pass cn = ClassName() print(isinstance(a,int)) # True print(isinstance(b,list)) # True print(isinstance(cn,ClassName)) # True print(isinstance(a,(str,int,list))) # 是元组中的一个返回 True
type()&isinstance()的区别:
-
type() 不会认为子类是一种父类类型,不考虑继承关系。
-
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
class A(object): pass class B(A): pass a = A() # 如果是经典类,type(a) == instance b = B() print(isinstance(a,A)) # True print(type(a)) # 如果是经典类,type(a)--> <type 'instance'> print(type(a) == A) # True print(isinstance(b,A)) # True print(type(b) == A) # False


浙公网安备 33010602011771号