1. 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,…
filter built-in function filter(f,sequence) filter can apply the function f to each element of sequence. If return is true the element will be returned and re-organized to be a new sequence. The type of new sequence can be list/tuple/string, this dep…
filter(function, iterable): Construct a list from those elements of iterable for which function returns true. 对iterable中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于iterable的类型)返回. iterable包括列表,iterator等.一个简单例子,过滤出一个整数列表中所有的奇数 >>&…
# lambda,filter,map,reduce from functools import reduce print('返回一个迭代器') print((x) for x in range(5)) print('迭代器转换为tuple') print(tuple((x) for x in range(5))) print('.......') print('匿名函数lambda传参方式一') print((lambda x, y: x+y)(1, 2)) print((lambda x:…
高阶函数 lambda函数 关键字lambda表示匿名函数,当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. lambda函数省略函数名,冒号前为参数,冒号后函数体. # 定义一个取偶数的函数 def even(x): for i in x: if i % 2 == 0: yield i # 等价于取偶数 lambda x: x % 2 == 0 filter函数 filter(function,iterable)接收一个函数和一个可迭代对象作为参数,过滤iterab…
''' Python --version :Python 2.7.11 Quote : https://docs.python.org/2/tutorial/datastructures.html#more-on-lists Add by camel97 2017-04 ''' 1.filter() #filter(function, sequence) returns a sequence consisting of those items from the sequence for whic…