Python list function build in three useful funtion
(1):filter(funtion,sequence)
This function will return the item in the sequence invoke the function and return true
such as:
def f(x):
if x > 0:
return True
else:
return False
filter(f,range(-3,4))
will return(1,2,3)
(2):map(function,sequence[,sequence...])
This funcion will map all the sequence one by one, so the sequence list's size must be all the same.
for example:
def m(x,y,z):
return x+y+z
map(f,range(3),range(3),range(3))
will return
[0,3,6]
so, how many params the function has. how many sequence are needed
(3):reduce(function,sequence)returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item, and so on.
for example:
>>> def r(x,y):
print 'x is %s, y is %s' % (x,y)
>>> reduce(r,range(5))
x is 0, y is 1
x is None, y is 2
x is None, y is 3
x is None, y is 4
So the function must take two params,
and there is a third param, use it to set the first param
for example:
>>> reduce(r,range(5),1)
x is 1, y is 0
x is None, y is 1
x is None, y is 2
x is None, y is 3
x is None, y is 4
分享到:
相关推荐
Python内置了一些非常有趣、有用的函数,如:filter、map、reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车。 1. filter函数的功能相当于...
这篇文章主要介绍了简单了解python filter、map、reduce的区别,...filter,map,reduce为内置函数。 lambda:实现python中单行最小函数。 g = lambda x: x * 2 #相当于 def g(x): return x*2 filter(function, se
本文实例讲述了Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法。分享给大家供大家参考,具体如下: 函数的返回值: 函数一旦执行到 return,函数就会结束,并会返回return 后面的值,...
详解python中三种高阶函数(map,reduce,filter)
文章目录sorted函数一、sort方法二、sorted内置函数三、情景引入filter类一、简单使用二、练习map类语法:一、简单使用二、练习reduce函数语法:一、简单使用二、设置初始值 Python中使用函数作为参数的内置函数和类...
主要介绍了Python lambda表达式filter、map、reduce函数用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本篇是对Python中的匿名函数和map()函数、reduce()函数、filter()函数这四三大封装函数(遍历处理),以及它们的使用案例。 文末是对这几个函数用法功能的比较。 匿名函数 该函数的用法类似于C语言中的宏定义,只是这...
python 高阶函数:map reduce filter map函数:遍历序列,对序列中每个元素进行操作,最终获取新的序列。 reduce函数:对于序列内所有元素进行累计操作。 filter函数:对于序列中的元素进行筛选,最终获取符合条件的...
这篇文章主要介绍了Python3的高阶函数map,reduce,filter的示例代码,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,...
内置高阶函数 map: reduce: filter: sorted: 函数式编程 首先来看一段函数式编程的定义,不想看的先跳过,结合具体实例后再来理解吧! 函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数...
python中有许多内置函数,不像print那么广为人知,但它们却异常的强大,用好了可以大大提高代码效率。 这次来梳理下8个好用的python内置函数。 1、set() 当需要对一个列表进行去重操作的时候,set()函数就派上用场了...
以内置的max函数为例子,查看其doc:复制代码 代码如下:>>> print max.__doc__max(iterable[, key=func]) -> valuemax(a, b, c, …[, key=func]) -> value With a single iterable argument, return its largest ...
在Python内建函数中,有一个和map()函数用法类似、却可以用来过滤元素的迭代函数,这个函数就是filter()。它的函数原型是:filter(function,itearable) filter返回的是一个filter对象,可以通过list()或者for循环...
python的map 函数使得函数能直接以list的每个元素作为参数传递到funcname中, 并返回响应的新的list 如下: def sq(x): return x*x #求x的平方 map(sq, [1,3, 5,7,9]) #[1, 9, 25, 49, 81] 在需要对list中的每个...
map()会根据提供的函数对指定序列进行映射,python3会返回一个迭代器,具体用法如下: def double(x): return 2*x if __name__=="__main__": print(map(double,[1,2,3,4,5])) print() for i in map(double,[1,2...