day13——重要内置函数、匿名函数、闭包
day13
内置函数2
重要的
abs():求绝对值——返回的都是正数
# lst = [-1,-2,-3]
# for i in lst:
# print(abs(i))
# print([abs(i) for i in lst])
# s = -123
# print(abs(s))
enumerate('可迭代对象','序号的起始值'):枚举,默认的起始值是0
# lst = [-1, -2, -3]
# print([i for i in enumerate([abs(i) for i in lst])])
# lst = [11, 2, 3, 4, 5, 6, 7]
# new_lst = []
# for i in enumerate(lst):
# new_lst.append(i)
# print(new_lst)
# print([i for i in enumerate(lst,1000)])
max('可迭代对象',key = abs):求最大值,比的是当前编码集的值
# lst = [11, 2, 3, 4, 5, 6, 7,1231,2131]
# print(max(lst))
min('可迭代对象',key = abs):取最小值
# lst = [11, 2, 3, 4, 5, 6, 7,1231,2131]
# print(min(lst))
sum([1,2,3,4,5],100(前面总数加这个数)):求和
# lst = [11, 2]
# print(sum(lst,100)) # 结果是113
print(sep=‘ ’,end=‘\n’) sep多个元素的连接符
# print(sep=' ', end='\n') # 默认
# print(1,2,3,sep='$')
# print(1,2,3,end='%')
# print(4,5)
range()
# python3:
# g = range(0,10) # 可迭代对象
# g.__iter__()
# python2:
# range(0,10) # 获取是一个列表
# xrange(0,10) # 获取是一个可迭代对象
---------------------------------------------------------
# from collections import Iterable,Iterator
# print(isinstance(g,Iterable))# 判断是否是可迭代对象
# print(isinstance(g,Iterator))# 判断是否是迭代器
open():写入文件
# print(12345,file=open("t1.txt","w",encoding="utf-8"))
list()
# print(list('alex')) # 结果是['a', 'l', 'e', 'x']
dict()
# print(dict(key=1,a='alex'))
# print(dict(((1,2),(2,3),(3,4))))
# print(dict([i for i in enumerate(range(20),1)]))
zip('可迭代对象','可迭代对象'):拉链,按照最少的进行合并
# lst1 = [1,2,3,4,5]
# lst2 = ['a',"b","c","d","f","e"]
# print(dict(list(zip(lst1,lst2))))
# print(dict(zip(lst1,lst2)))
dir():查看当前函数的方法
# print(dir(list)) # 查看当前函数的方法
匿名函数——一行函数
f = lambda x,y:x+y
print(f(1,2))
f = lambda x,y:(x,y)
print(f(1,2)) # 结果为元组
print((lambda x:x)(2))# 同一行定义 同一行调用
匿名函数的名字就是lambda
lambda 关键字——定义函数
x,y:形参——可以不写
x+y:返回值(必须写,可以写None)——只能返回一个数据类型
# f = lambda x,y:x+y
# print(f(1,2))
# print((lambda x,y:x+y)(1,2))
# print(type((lambda x,y:(x,y))(1,2)))
---------------------------------------------------------
# f = lambda x,y:x+y
# print(f(1,2))
# print(f.__name__)
# print((lambda x:x)(2))
---------------------------------------------------------
# lst = [lambda i:i*i for i in range(10)]
# print(lst[2](20))
# lst = []
# for i in range(10):
# def func(i):
# return i*i
# lst.append(func)
# print(lst[2](20))
---------------------------------------------------------
# lst = [lambda :i*i for i in range(10)]
# print(lst[2]())
# lst = []
# for i in range(10):
# def func():
# return i*i
# lst.append(func)
# print(lst[2]())
---------------------------------------------------------
# lst = list((lambda i:i*i for i in range(5)))
# print(lst[1](55))
# lst = [x for x in (lambda :i**i for i in range(5))]
# print(lst[2]())
---------------------------------------------------------
# lst = []
# def func():
# for i in range(5):
# def foo():
# return i**i
# yield foo
# for x in func():
# print(x)
# lst.append(x)
# print((lst[2]()))
最重要的内置函数(面试比较重要的)
format()函数
format(13,>20):右对齐(不常用)
format(13,<20):左对齐(不常用)
format(13,^20):居中(不常用)
format(13,'08b'):转换成二进制——08(可有可无)
format(13,'08d'):转换成十进制——08(可有可无)
format(13,'08o'):转换成八进制——08(可有可无)
format(13,'08x'):转换成十六进制——08(可有可无)
# print(format(13,">20")) # 右对齐
# print(format(13,"<20")) # 左对齐
# print(format(13,"^20")) # 居中
------------------------------------
# print(format(13,"08b")) # 2
# print(format(13,"08d")) # 10
# print(format(13,"08o")) # 8
# print(format(12,"08x")) # 16
filter():过滤
filter('函数','可迭代对象')
写函数的时候可以指定过滤条件
# lst = [1,2,3,4,5,8,5,6,76,7,7]
# def func(s):
# return s > 3
# print(list(filter(func,lst)))
# func就是自己定义一个过滤条件,lst要迭代的对象
# print(list(filter(lambda s:s>3 ,lst)))
map():对象映射
map('函数','可迭代对象')
对可迭代对象中每个元素进行加工
# lst = [1,2,3,4,5]
# def func(s):
# return s*s
# mp = map(func,lst)
# print(mp)
# print(list(mp))
--------------------------------------
# print(list(map(lambda s:s*s,lst)))
reversed():翻转
对可迭代对象进行翻转
支持所有可迭代对象(有序),原数据不变
# lst = [1,2,3,4,5]
# lst1 = list(reversed(lst))
# print(lst)
# print(lst1)
sorted():排序
sorted('可迭代对象',key='函数名',reverse = True)
key是指定排序的规则
# lst = [1,23,34,4,5,213,123,41,12,32,1]
# print(sorted(lst)) # 升序
# print(lst)
# print(sorted((lst),reverse=True)) # 降序
# dic = {"key":1,"key1":78,"key3":56}
# print(sorted(dic,key=lambda x:dic[x],reverse=True))
不用加list(),默认升序,加了reverse = True变成降序,原数据不变
reduce:累计算
reduce('函数','可迭代对象')
# from functools import reduce
# print(reduce(lambda x,y:x-y,[1,2,3,4,5]))
高阶函数(有key):sorted、filter、map、max、min、reduce
闭包
-
闭包:在嵌套函数内,使用非全局变量(且不是本层变量)就是闭包
-
闭包的作用:
保证数据的安全性
装饰器的本质
-
验证是否是闭包:closure
了解:
# print(ret.__code__.co_freevars) # 获取的是自由变量
# print(ret.__code__.co_varnames) # 获取的是局部变量
# def func():
# a = 1
# def f1():
# def foo():
# print(a)
# return foo
# return f1
# ret = func()
# a = ret()
# a()
# func()()()
---------------------------------------------------------
# avg_lst = []
# def func(pirce):
# avg_lst.append(pirce)
# avg = sum(avg_lst) / len(avg_lst)
# return avg
# print(func(150000))
# print(func(160000))
# print(func(170000))
# print(func(150000))
# print(func(150000))
---------------------------------------------------------
# def func():
# avg_lst = []
# def foo(pirce):
# avg_lst.append(pirce)
# avg = sum(avg_lst) / len(avg_lst)
# return avg
# return foo
# ret = func()
# print(ret(150000))
# print(ret(160000))
# print(ret(170000))
# print(ret(150000))
# print(ret(180000))
# print(ret.__closure__) # (<cell at 0x0030F150: list object at 0x002B35A8>,)