filter和map的区别

相同点:filter 和 map 都是对数组的操作,均返回一个新的数组
不同点:filter是满足条件的留下,是对原数组的过滤;map则是对原数组的加工,映射成一对一映射的新数组

 

number = [1,2,44,38,58,96,47]
new_list = list(filter(lambda x: (x%2 == 0),number))
print(number)
print(new_list)

my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2, my_list))
print(my_list)
print(new_list)

[1, 2, 44, 38, 58, 96, 47]
[2, 44, 38, 58, 96]
[1, 5, 4, 6, 8, 11, 3, 12]
[2, 10, 8, 12, 16, 22, 6, 24]

  

posted @ 2020-01-07 19:39  lucky_tomato  阅读(4623)  评论(0编辑  收藏  举报