python--两个牛逼的BIF,内置函数

>>> help(filter)
Help on class filter in module builtins:

class filter(object)
 |  filter(function or None, iterable) --> filter object
 |  
 |  Return an iterator yielding those items of iterable for which function(item)
 |  is true. If function is None, return the items that are true.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.

>>>

#过滤掉非True或者False的,

>>> filter(None,[1,0,False,True])

>>> list(filter(None,[1,0,False,True]))
[1, True]
>>>

求一到十的奇数

>>> def odd(x):
    return x % 2

>>> temp = range(10)

#filter()第一个参数为函数名字或者None,后面跟要过滤的数组

>>> show = filter(odd,temp)
>>> list(show)
[1, 3, 5, 7, 9]
>>> show

>>> list(filter(lambda x : x % 2,range(10)))
[1, 3, 5, 7, 9]
>>>


map() 第一个参数处理,第二个传入的数据,返回处理过的数据

>>> def func(x):
    return x * 2

>>> list(map(func ,[0,1,2,3,4,5]))
[0, 2, 4, 6, 8, 10]
>>>

>>> list(map(lambda x : x * 2,range(10)))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>



你可能感兴趣的:(Python)