list, tuple, or string Return those items of sequ_过滤器怎么处理参数">
赞
踩
- def filter(function_or_none, sequence): # known special case of filter
- """
- filter(function or None, sequence) -> list, tuple, or string
-
- Return those items of sequence for which function(item) is true. If
- function is None, return the items that are true. If sequence is a tuple
- or string, return the same type, else return a list.
- """
- pass
- def func_filter(x):
- if x > 0:
- return True
- else:
- return False
- filter(func_filter,[-1,0,1])
- filter(func_filter,'abc')
- filter(func_filter,(-1,0,1))
结果:
不能改造filter原的方法,我们可以改变自己的函数传参类型,从而控制函数可以使用filter方法
- def func_filter(vals):
- if len(vals) == 2 and vals[0] > 0 and vals[1] > 0:
- return True
- else:
- return False
- filter(func_filter,[[-1,-1],[0,0],[1,1]])
- filter(func_filter,'abc')
- filter(func_filter,((-1,-1),(0,0),(1,1)))
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。