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 depends on the input sequence type.

>>> l=[1,2,3,4,5]
>>> f = lambda x: True if x%2 == 0 else False
>>> filter(f,l)
[2, 4]
>>> t=(1,2,3,4,5)
>>> filter(f,t)
(2, 4)

map built-in function

map(f, sequence)

map will apply f to each element of sequence. The result will be returned to be a new list(Unless filter, the return will always a list).

>>> f= lambda y : y*2
>>> l=[1,2,3]
>>> t=(1,2,3)
>>> map(f,l)
[2, 4, 6]
>>> map(f,t)
[2, 4, 6]

reduce() built-in function

reduce(f, sequence, start_value)

reduce apply f to sequence by the order of the sequence. If there is a start_value, this one will be called first.

>>> l = [1,2,3,4,5,6,7,8,9]
>>> f = lambda x,y : x+y
>>> reduce(f,l)
45
>>> reduce(f,l, 20)
65

python: filter, map, reduce, lambda的更多相关文章

  1. filter,map,reduce,lambda(python3)

    1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...

  2. Python2.7学习笔记-定义函数、filter/map/reduce/lambda

    我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...

  3. python filter map reduce

    filter(function, iterable): Construct a list from those elements of iterable for which function retu ...

  4. Python中特殊函数和表达式 filter,map,reduce,lambda

    1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...

  5. Python中 filter | map | reduce | lambda的用法

      1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...

  6. Python学习(五)函数 —— 内置函数 lambda filter map reduce

    Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...

  7. Python经常使用内置函数介绍【filter,map,reduce,apply,zip】

    Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...

  8. Python内置函数之filter map reduce

    Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...

  9. Python之匿名函数(filter,map,reduce)

    参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...

随机推荐

  1. 微信小程序组件解读和分析:九、form表单

    form表单组件说明: 表单,将组件内的用户输入的<switch/> <input/> <checkbox/> <slider/> <radio/ ...

  2. iOS---数据离线缓存

    离线缓存 为了用户的体验,不需要每次打开App都加载新数据,或者重新请求数据,因此需要把每次浏览的数据保存起来,当下次打开软件时,首先从沙盒中加载数据:或者当软件未联网时,也只能从沙盒中加载旧数据. ...

  3. C#方法参数关键字

    一.params关键字 prams告诉函数的调用者,该函数的参数数量是可变,如果调用函数的参数标识了params关键字,那么我们可以使用逗号分割的参数或者一个数组来作为参数: 1.这里只能是数组,Li ...

  4. 理清楚HTML和DHTML和XML的概念

    DHTML 不是 W3C 标准DHTML 指动态 HTML(Dynamic HTML).DHTML 不是由万维网联盟(W3C)规定的标准.DHTML 是一个营销术语 - 被网景公司(Netscape) ...

  5. Cygwin, MinGW/MSYS, MinGW-W64/MSYS2

    1. Cygwin http://www.cygwin.com/ Cygwin is a large collection of GNU and Open Source tools which pro ...

  6. 自定义对话框(jDialog)

    [配置项]jDialog options点击收起 一.接口功能 jDialog的默认配置项,本组件提供的所有对话框,都可以通过修改这些配置项来实现不同的效果. 二.详细配置项 /** * 对话框的默认 ...

  7. laravel如何自定义控制器目录

    默认控制器在App\Http\Controllers目录下,如何自定义目录呢? 首先我们看一下laravel的请求周期 我们看一下服务提供者RouteServicePrivder文件中的一个函数 /* ...

  8. LeetCode137只出现一次的数字——位运算

    题目 题目描述:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现三次.找出那个只出现一次的元素. 说明:你的算法应该具有线性时间的复杂度.你可以不使用额外的空间来实现吗? 思路 题 ...

  9. CLUSTER - 根据一个索引对某个表集簇

    SYNOPSIS CLUSTER indexname ON tablename CLUSTER tablename CLUSTER DESCRIPTION 描述 CLUSTER 指示PostgreSQ ...

  10. 学习React从接受JSX开始

    详情参考官方JSX规范 虽然JSX是扩展到ECMAScript的类XML语法,但是它本身并没有定义任何语义.也就是说它本身不在ECMAScript标准范围之内.它也不会被引擎或者浏览器直接执行.通常会 ...