码迷,mamicode.com
首页 > 编程语言 > 详细

2.python函数编程-filter函数

时间:2018-10-14 11:23:21      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:pen   pytho   star   end   filter   col   函数实现   lis   lex   

fileter功能主要使用在需要对数据进行多种操作,并对数据进行过滤的操作。

普通函数实现:

 1 movie = [sb_alex, wupei, tiger, goosb,xxfd,sb_xxx]
 2 def fileter_peope(func,array):
 3     ret=[]
 4     for i in array:
 5         if not  func(i):
 6             ret.append(i)
 7     return ret
 8 
 9 res = fileter_peope(lambda n:n.startswith("sb"),movie)
10 print(res)

执行结果

[‘wupei‘, ‘tiger‘, ‘goosb‘, ‘xxfd‘]

 使用filter函数实现

1 movie = [sb_alex, wupei, tiger, goosb,xxfd,sb_xxx]
2 res = filter(lambda n:not n.startswith("sb"),movie)
3 print(res)
4 print(list(res))

执行结果

<filter object at 0x0000014FFBDC4B70>
[‘wupei‘, ‘tiger‘, ‘goosb‘, ‘xxfd‘]

 

2.python函数编程-filter函数

标签:pen   pytho   star   end   filter   col   函数实现   lis   lex   

原文地址:https://www.cnblogs.com/chrrydot/p/9784997.html

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