>>> #
filter函数可以对返回布尔值的函数进行元素过滤为真的元素列表
>>> def fu(x):
return x.isdigit()#isdigit 判断是否为纯数字
>>> li=['Boy','41','Tim','QQ','1125891']
>>> filter(fu,li)
['41', '1125891']
>>>
>>> li=('Boy','41','Tim','QQ','1125891')
>>> filter(fu,li)
('41', '1125891')
>>> li = 'boy 41 tim qq 112'
>>> filter(fu,li)
'41112'
>>> li = 'boy-41-tim-qq-112'
>>> filter(fu,li)
'41112'
可以用内置函数__doc__或者help()查看filter的说明:
>>> print filter.__doc__
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.
>>>
支持列表、元组、字符串。
传入的参数是什么类型输出参数就是什么类型,对字符串会以空格等字符的自动分割(这应该跟我在函数里面的isdigit函数有关),结果会拼接为字符串。