python filter map reduce
Construct a list from those elements of iterable for which function returns true.
-
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value
# -*- coding: utf-8 -*-
def test():
lst = [[1,2,3], [5], [5, 6]]
print ' ---- use filter ----'
ret = []
print filter(lambda e: ret.extend(e), lst)
print ret print ' ---- use map ----'
ret = []
print map(lambda e: ret.extend(e), lst)
print ret print ' ---- use reduce ----'
ret = reduce(lambda r, e: r.extend(e) or r, lst, [])
print ret print ' ---- use itertools.chain ----'
import itertools
print list(itertools.chain(*lst)) if __name__ == '__main__':
test()
最后,只要可以,尽量使用list comprehension。
python filter map reduce的更多相关文章
- python: filter, map, reduce, lambda
filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
- Python: lambda, map, reduce, filter
在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
Python是一门非常简洁,非常优雅的语言,其非常多内置函数结合起来使用,能够使用非常少的代码来实现非常多复杂的功能,假设相同的功能要让C/C++/Java来实现的话,可能会头大,事实上Python是 ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- filter,map,reduce,lambda(python3)
1.filter filter(function,sequence) 对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个lis ...
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- python基础——map/reduce
python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...
随机推荐
- 爬虫入门 手写一个Java爬虫
本文内容 涞源于 罗刚 老师的 书籍 << 自己动手写网络爬虫一书 >> ; 本文将介绍 1: 网络爬虫的是做什么的? 2: 手动写一个简单的网络爬虫; 1: 网络爬虫是做 ...
- [转载] Dubbo架构设计详解
转载自http://shiyanjun.cn/archives/325.html Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合 ...
- sysbench
安装 http://www.cnblogs.com/zhoujinyi/archive/2013/04/19/3029134.html http://space.itpub.net/758322/vi ...
- cgg之数据类型
所有例子都在64为操作系统 Linux 2.6.30 x86_64 x86_64 x86_64 GNU/Linux 1.1整数 在stdint.h中定义一些看上去更明确的整数类型 #ifndef __ ...
- vpn服务器搭建
这里我们用CentOS6.0和Shdowsocks搭建 首先了解几个命令 wget 是一个从网络上自动下载文件的自由工具,支持通过 HTTP.HTTPS.FTP 三个最常见的 TCP/IP协议 下载, ...
- CVE-2017-11882漏洞利用
CVE-2017-11882漏洞利用 最新Office的CVE-2017-11882的poc刚刚发布出来,让人眼前一亮,完美无弹窗,无视宏,影响Ms offcie全版本,对于企业来说危害很大.在此简单 ...
- C语言之scanf
#include<stdio.h>int main(){int num;int a,b,c,result,d,result1;scanf("int%d",&nu ...
- 基于iframe的移动端嵌套
需求描述 上上周接到了新的项目,移动端需要做一个底部有五个导航,点击不同的导航页面主体显示不同的页面,其中两个页面是自己做,而另外三个页面是引用另外三个网址,其中两个网址为内部项目,另外一个为外部(涉 ...
- Shell 初步学习
Shell 概述 Shell:Linux命令解释器 脚本执行方式 chmod 755 脚本名:赋权限(调用必须显示的使用绝对路径或相对路径) bash 脚本名:通过Bash调用执行脚本 命令别名 al ...
- phpstorm快捷键记录
快捷键记录 Ctrl + N 按类名查找Ctrl + Shift + N 按文件名查找,快速查找文件Ctrl + Shift+Alt+N 根据函数名查找Ctrl + F 当前文件查找Ctrl + Sh ...