匿名函数 == 一行函数
# lst =[lambda x:x+i for i in range(5)]
# for i in lst:
# print(i(1))
# lst = (lambda x: x + i for i in range(5))
# for i in lst:
# print(i(1))
# print((lambda x:x+6)(5))11
# s=(i for i in range(3))
# for i in s:
# print(i)
# f = lambda x:x+6#函数名叫lambda()
# print(f.__name__)
# f = lambda x,y,z,b=1:(x,y,z,b)#只能返回一个值
# # print(f(1,2,3))
# print([lambda :5][0]())
# print((lambda :5)())
# a = lambda :5#这种比较常用
# print(a())
# lst = []
# for i in range(5):
# def func():
# return i
# lst.append(func)
# print(lst[0]())4
# lst = [] # [lambda x:x+1,lambda x:x+1]
# for i in range(2):
# lst.append(lambda x:x+1)
# print(lst[-1](5))6
#
# lst = [lambda x:x+1 for i in range(5)]
# print(lst[0](5))6
# tu = (lambda :i for i in range(3))
# print(next(tu)())
# print(next(tu)())
# print(next(tu)())
# tu = (lambda :i for i in range(3))
# for i in tu:
# print(i())
# def func():
# for i in range(3): # i = 0 1 2
# def foo(): # foo1 foo2 foo3
# return i
# yield foo # foo1 foo2 foo3
# g = func()
# print(next(g)()) # foo1
# print(next(g)) # foo2
# print(next(g)) # foo3
# tu = (lambda :i for i in range(3))
# print(next(tu)())
#
# lst = [lambda x:x+5 for i in range(2)]
# print([i(2) for i in lst])#[7, 7]
# lst = [] # [lambda x:x+5,lambda x:x+5]
# for i in range(2):
# lst.append(lambda x:x+5)
# new_lst = []
# for i in lst:
# new_lst.append(i(2))
# print(new_lst)
# lst = (lambda x:x+5 for i in range(2))
# print([i(2) for i in lst])
# def func():
# for i in range(2):
# f = lambda x: x + 5
# yield f
# g = func()
# lst = []
# for i in g:
# lst.append(i(2))
# print(lst)
# lst = [lambda x:x*i for i in range(2)]
# print([i(2) for i in lst]) #[2,2]
# lst = [] # [lambda x:x*i,lambda x:x*i]
# for i in range(2):
# lst.append(lambda x:x*i)
# # print(i)
# new_lst = []
# for em in lst:
# new_lst.append(em(2))
# print(new_lst)
# def func():
# for i in range(2):
# f = lambda x:x*i
# yield f
# g = func()
# lst = []
# for i in g:
# lst.append(i(2))
# print(lst)
# func = lambda x:[i for i in x]
# print(func('afafasd'))
#
# print(list('afafasd'))
# print(dict([(1, 2), (3, 4)]))
# dic1 = {"key1":1,"key2":2}
# dic2 = {"a":1,"b":2}
# dic2.update(dic1)
# print(dic2)
内置函数2
#字典的转化
# print(dict(((1,2),(3,33))))
# print(dict([(1,2),(3,4)]))
# dic1 = {"key1":1,"key2":2}
# dic2 = {"a":1,"b":2}
# dic2.update(dic1)
# print(dic2)
# print(dict(**dic1,**dic2))
# 注意
# def cen(*args,**kwargs):
# print(kwargs)
# print(args)
# lis=[1,2,3,4,5]
# dic={"1":"2","3":"4"}
# cen(*lis,**dic)
# print(dict(**dic))
# sep : 每一个元素之间分割的方法 默认 " " ****
# print(1,2,3,sep="|")
# end :print执行完后的结束语句 默认
****
# print(1,2,3,end="")
# print(4,5,6,end="")
# print(1,2,3,sep="")#123
# file : 文件句柄 默认是显示到屏幕
# print(1,2,3,file=open("test","w",encoding="utf-8"))
# print(sum([1,2,2,1]))
# print(sum([10,20,30,40],100))
# print(abs(9)) # 绝对值
# print(dir(list)) # 查看当前对象所有方法 返回的是列表
# print(dir(str)) # 查看当前对象所有方法
# lst1 = [1,2,34,5]
# lst2 = ["alex","wusir","宝元"]
# lst3 = [2,2,2,2,2]
# print(list(zip(lst1,lst2,lst3))) # 拉链
# 面试题
# lst1 = [1,2,34,5]
# lst2 = ["alex","wusir","宝元"]
# print(dict(zip(lst1,lst2)))
# print(format("alex",">20")) # 右对齐
# print(format("alex","<20")) # 左对齐
# print(format("alex","^20")) # 居中
# print(format(10,"b")) # bin 二进制
# print(format(10,"08b"))
# print(format(10,"08o")) # oct 八进制
# print(format(10,"08x")) # hex 十六进制
# print(format(0b1010,"d")) # digit 十进制
# lst = [1,2,3,4,5]
# print(list(reversed(lst))) # 反转
# print(lst)
#filter==过滤
# lst = [1,2,3,4,5,6]
# print(list(filter(lambda x:x>1,lst)))
# print(list(filter(lambda x:x>2,[1,2,3,4,5])))
# lst = [{'id':1,'name':'alex','age':18},
# {'id':1,'name':'wusir','age':17},
# {'id':1,'name':'taibai','age':16},]
#
# print(list(filter(lambda x:x['age']>16,lst)))
#映射==map
# print(list(map(lambda x,y:x+y,[1,2,3,4,5],[33,22,44,55])))
# sorted() -- 排序
# print(sorted([1,2,3,4,5,6],reverse=True))
# print(sorted([1,2,3,4,5,-6],reverse=True,key=abs))
# lst = ["三国演义","红楼梦","铁道游击队","西游记","水浒传","活着"]
# print(sorted(lst,key=len))
# lst = [{"age":19},{"age1":20},{"age2":80},{"age3":10}]
# print(sorted(lst,key=lambda x:list(x.values())))
# print(sorted(lst,key=lambda x:list(x.keys()),reverse=True))
# help(sorted)
# lst = [{'id':1,'name':'alex','age':18},
# {'id':2,'name':'wusir','age':17},
# {'id':3,'name':'taibai','age':16},]
#
# print(sorted(lst,key=lambda x:x['age']))
# max() -- 最大值
# print(max(10,12,13,15,16))
# print(max([10,12,13,15,-16],key=abs))
# min() -- 最小值
# print(min(10,12,13,15,16))
# print(min([10,12,13,15,-16],key=abs))
# from functools import reduce # 累计算
# 从 functools工具箱中拿来了reduce工具
# def func(x,y):
# return x+y
# print(reduce(func,[1,2,3,4,5]))
# print(reduce(lambda x,y:x+y,[1,2,3,4,5]))
闭包
# 1.在嵌套函数中,使用非本层且非全局变量的就是闭包
# print(inner.__closure__) 判断是否是闭包 返回None就不是闭包
# 闭包的作用:
# 1.保护数据的安全性
# 2.装饰器
总结
# 1.匿名函数
# lambda x:x
# 定义函数的关键字
# x: 形参 (可以写多个,可以不写)
# :x 返回值 (必须写,只能写一个数据类型)
# 面试题:
# 2.内置函数二
# dict(k=1)
# dict([(1,2)])
# dict(**dic1,**dic2)
# print()
# sep : 每个元素的分割 默认是空格
# end : print语句的结束语句 默认是
# sum()求和 可迭代对象容器,容器的元素必须是数字
# sum([1,2,34,5,6,7],100) 指定开始位置的值
# abs() 绝对值
# dir() 查看当前对象所有方法
# format() > < ^ o b x d
# zip() 拉链 -- 当长度不一致时选择最短的进行合并
# 面试题:
# dict(zip(list1,list2))
# reversed() 反转
# filter() - 过滤 函数名(指定过滤规则),可迭代对象
# map() -- 映射 函数名(对每个元素执行的方法),可迭代对象
# sorted() 可迭代对象,key=指定规则
# max() 可迭代对象,key=指定规则
# min() 可迭代对象,key=指定规则
# reduce 累计算
# 3.闭包
# 1.在嵌套函数中,使用非本层且非全局变量的就是闭包
# print(inner.__closure__) 判断是否是闭包 返回None就不是闭包
# 闭包的作用:
# 1.保护数据的安全性
# 2.装饰器