filter()的使用

 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']
正是江南好风景
原文地址:https://www.cnblogs.com/monsterhy123/p/12896117.html