一、匿名函数:
p=lambda x:x+'_sb' print(p('alex'))
#输出结果:alex_sb
p=lambda x,y,z:(x+1,y+2,z+3) print(p(4,5,6))
#输出结果:(5, 7, 9)
def calc(x): return x+1 print(calc(10)) #输出结果:11
lambda x:x+1 p=lambda x:x+1 print(p(10)) #输出结果:11
二、reduce函数:
num_1=[1,2,3,100] def reduce_test(func,array): res=1 for num in array: res=func(res,num) return res print(reduce_test(lambda x,y:x*y,num_1)) #输出结果:600
num_1=[1,2,3,100] def reduce_test(func,array): res=array.pop(0) ######## for num in array: ###这时,array已经变成了[2,3,100] res=func(res,num) return res print(reduce_test(lambda x,y:x*y,num_1)) #输出结果:600
三、map函数:
num_list=[1,2,3,4,5] p=[] for i in num_list: p.append(i**2) print(p) #输出结果:[1, 4, 9, 16, 25]
num_list=[1,2,3,4,5] def map_test(arry): ret=[] for i in num_list: ret.append(i**2) return ret ret=map_test(num_list) print(ret) #输出结果:[1, 4, 9, 16, 25]
list=[1,2,3,4,5] def addone(x): return x+1 def reduceone(x): return x-1 def map_test(function,array): #给这个函数里面增加了两个形参 ret=[] for i in list: res=function(i) ret.append(res) return ret # print(map_test(addone,list)) print(map_test(lambda x:x+1,list)) #输出结果:[2, 3, 4, 5, 6]
num_1=[1,2,3,4,5] map(lambda x:x+1,num_1) res=map(lambda x:x+1,num_1) print(list(res)) #输出结果:[2, 3, 4, 5, 6]
msg='alexdjd' res=map(lambda x:x.upper(),msg) print(list(res)) #输出结果:['A', 'L', 'E', 'X', 'D', 'J', 'D']
四、内置函数:
print(list(zip(('a','b','c'),(1,2,3)))) #输出结果:[('a', 1), ('b', 2), ('c', 3)]
name=('a','b','c') print(min(name)) #输出结果:a
五、函数式编程:
a=1 def incr_test1(): global a a+=1 return a incr_test1() print(a) #输出结果:2
# # 函数即变量(_把函数当做参数传给另外一个函数) def foo(n): print(n) def bar(name): print('my name is %s'%name) # foo(bar) foo(bar('alex')) #输出结果: my name is alex None
#返回值当中包含函数 def foo(): print('from foo') return foo foo() #返回结果:from foo
#返回值当中包含函数 def bar(): print('from bar') def foo(): print('from foo') return bar foo() p=foo() print(p) p() #输出结果: from foo from foo <function bar at 0x01D524B0> from ba
# #返回值当中包含函数 def handle(): print('from handle') return handle # handle() # p=handle() # # print(p) p=handle() p() #输出结果: from handle from handle
def test1(): print('from test1') def test2(): print('from test2') # return test1 return test1() test2() #输出结果: from test2 from test1
def handle(): print('from handle') return handle() # handle() # p=handle() # # print(p) p=handle() p() #报错!!
def bar(): print('from bar') def foo(): print('from foo') return bar() foo() p=foo() print(p) p() #报错!!
六、文件操作
1.读模式:
首先创建一个test文件,命名为“李佳伦”。再分别执行下面的命令,观察结果。
# f=open('李佳伦',encoding='utf-8') # data=f.read() # print(data) # f=open('666',) # data=f.read() # print(data) # f=open('李佳伦','r',encoding='utf-8') # data=f.readline() # print(data) # print(f.readline()) # print(f.readline()) # print(f.readline()) # print(f.readline()) # # f=open('李佳伦','r',encoding='utf-8') # f.read() # print('第一行',f.readline()) # v=range(10) # for item in v: # print(item)
2.写模式:
f=open('李佳伦','w',encoding='utf-8') f.close() # 这时,李佳伦这个文件会被新的空文件替换掉!
3.追加模式:
f=open('xxx','r+',encoding='gbk') f.write('sb')
4.文件管理的其他的模式:
# src_f=open('xxx','r',encoding='gbk') # data=src_f.read() # src_f.close() # dst_f=open('xxx_new','w',encoding='gbk') # dst_f.write(data) # dst_f.close() # src_f=open('xxx','r',encoding='gbk') # data=src_f.readlines() # print(data) # src_f.close() # dst_f=open('xxx_new','w',encoding='gbk') # dst_f.writelines(data) # dst_f.close()