1 ###### map函数 处理序列中的每一个函数,得到的结果是一个’列表‘,该列表元素位置与原来一样 2 3 # num1 = [1,2,3,4,5,6,7,8,9] 4 # num2 = [] 5 # for i in num1: 6 # num2.append(i**3) 7 # 8 # print(num2) 9 10 # num1 = [1,2,3,4,5,6,7,8,9] 11 # def map_text(arrray): 12 # num2 = [] 13 # for i in num1: 14 # num2.append(i**3) 15 # return num2 16 # ret = map_text(num1) 17 # print(ret) 18 19 20 # num1 = [1,2,3,4,5,6,7,8,9] 21 # def add_text(x): 22 # return x-1 23 # 24 #def reduce_text(x): 25 # return x + 1 26 27 # def pingfang(x): 28 # return x**3 29 # 30 # def map_text(func,array): 31 # ret = [] 32 # for i in num1: 33 # res = func(i) 34 # ret.append(res) 35 # return ret 36 # 37 # lambda x:x+1 38 # lambda x:x-1 39 # lambda x:x**3 40 # 41 # 42 # print(map_text(add_text,num1) ) 43 # print(map_text(reduce_text,num1) ) 44 # print(map_text(pingfang,num1) ) 45 # print(map_text(lambda x:x+1,num1) ) 46 # print(map_text(lambda x:x-1,num1) ) 47 # print(map_text(lambda x:x**3,num1) ) 48 49 50 # array = [1,2,3,4,5,6,7,8,9] 51 # def map_text(func,array): 52 # ret = [] 53 # for i in array: 54 # res = func(i) 55 # ret.append(res) 56 # return ret 57 # 58 # print(map_text(lambda x:x+1,array) ) 59 # res = map(lambda x:x+1,array) 60 # print('内置函数map,处理结果',res ) 61 # 62 # print(list(res)) 63 64 # num1 = [1,2,3,4,5,6,7,8,9] 65 # def add_one(x): 66 # return x + 1 67 # print('臭弟弟,精神小伙',tuple(map(add_one,num1) )) 68 # print('臭弟弟,精神小伙',list(map(add_one,num1) )) 69 70 # msg = 'luoluo' 71 # print(list(map(lambda x:x.upper(),msg)))
1 ## filter() 函数 # filter函数,是用来过滤列表的。简单的说就是用一个函数来过滤一个列表, 2 # 把列表的每一项传递进入过滤函数,过滤函数返回false就从这个列表中删除该项。 3 4 # res = [] 5 # people = ['袁浩_sb','zhongyunyang_sb','liuyixiang_sb','kangkang'] 6 # for i in people: 7 # if not i.endswith('sb'): 8 # res.append(i) 9 # print(res) 10 11 12 # people = ['袁浩_sb','zhongyunyang_sb','liuyixiang_sb','kangkang'] 13 # def filter_text(array): 14 # res = [] 15 # 16 # for i in array: 17 # if not i.endswith('sb'): 18 # res.append(i) 19 # return res 20 # print(filter_text(people) ) 21 22 # people = ['袁浩_sb','zhongyunyang_sb','liuyixiang_sb','kangkang'] 23 # def sb_show(n): 24 # return n.endswith('sb') 25 # 26 # 27 # def filter_text(func,array): 28 # res = [] 29 # 30 # for i in array: 31 # if not i.endswith('sb'): 32 # res.append(i) 33 # return res 34 # print(filter_text(sb_show,people) ) 35 36 37 # people = ['袁浩_sb','zhongyunyang_sb','liuyixiang_sb','kangkang'] 38 # lambda n:n.endswith('sb') 39 # def filter_text(func,array): 40 # res = [] 41 # for i in array: 42 # if not i.endswith('sb'): 43 # res.append(i) 44 # return res 45 # a = filter_text(lambda n:n.endswith('sb'),people) 46 # print(a) 47 # 48 # print(list(filter(lambda n:not n.endswith('sb'),people) ))
1 #reduce函数## 2 3 # from functools import reduce 4 # num = [4,5,6,7] 5 # print(reduce(lambda x,y:x*y,num) ) 6 7 # num = [4,5,6,7] 8 # res = 0 9 # for i in num: 10 # res += i 11 # print(res) 12 13 14 # num = [4,5,6,7] 15 # def reduce_text(array): 16 # res = 0 17 # for i in num: 18 # res+=i 19 # return res 20 # print(reduce_text(num ) ) 21 #