码迷,mamicode.com
首页 > 其他好文 > 详细

map filter reduce 函数

时间:2020-03-11 11:03:05      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:import   span   lambda   进入   序列   ret   imp   简单的   nbsp   

 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 #

 

map filter reduce 函数

标签:import   span   lambda   进入   序列   ret   imp   简单的   nbsp   

原文地址:https://www.cnblogs.com/luoluokang/p/12460615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!