首页 > 其他 > 详细

filter()的使用

时间:2020-05-15 19:23:01      阅读:33      评论:0      收藏:0      [点我收藏+]
 1 # 在一个list中,删掉偶数,只保留奇数
 2 a = [1,2,3,4,5,6,7,8,9,10]
 3 def is_odd(n):
 4     return n % 2 == 1
 5 L = filter(is_odd,a)
 6 print(type(L))
 7 print(list(L))
 8 
 9 #filter序列中的空字符串删掉
10 a = [A,‘‘,B,None,C, ]
11 b = ‘‘ #b默认FALSE,None默认FALSE, 默认True 
12 def not_empty(s):
13     return s and s.strip() #去空格
14 L = filter(not_empty,a)
15 print(list(L))
1 <class filter>
2 [1, 3, 5, 7, 9]
3 [A, B, C]

 

filter()的使用

原文:https://www.cnblogs.com/monsterhy123/p/12896117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!