filter,map,reduce,lambda(python3)
1.filter
filter(function,sequence)
对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个list、string、tuple(根据sequence类型决定)返回。
- #!/usr/bin/env python
- # encoding: utf-8
- """
- @author: 侠之大者kamil
- @file: filter.py
- @time: 2016/4/9 22:03
- """
- #filter map reduce lambda
- def f1(x):
- return x % 2 != 0 and x % 3 != 0
- a = filter(f1, range(2,25))
- print(a)#<filter object at 0x7f7ee44823c8>
- #这种情况是因为在3.3里面,filter()的返回值已经不再是list,而是iterators.
- print(list(a))
- def f2(x):
- return x != "a"
- print(list(filter(f2,"dfsafdrea")))
结果:
- ssh://kamil@192.168.16.128:22/usr/bin/python3 -u /home/kamil/py22/jiqiao0406/fmrl.py
- <filter object at 0x7f4d1ca5e470>
- [5, 7, 11, 13, 17, 19, 23]
- ['d', 'f', 's', 'f', 'd', 'r', 'e']
- Process finished with exit code 0
2.map
语法与filter类似
- #!/usr/bin/env python
- # encoding: utf-8
- """
- @author: 侠之大者kamil
- @file: map.py
- @time: 2016/4/9 22:22
- """
- def cube(x):
- return x * x *x
- a = map(cube,range(10))
- print(list(a))
- def add(x,y,z):
- return x + y +z
- b = map(add,range(5),range(5),range(5))
- print(list(b))
结果:
- ssh://kamil@192.168.16.128:22/usr/bin/python3 -u /home/kamil/py22/jiqiao0406/map.py
- [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
- [0, 3, 6, 9, 12]
- Process finished with exit code 0
3.reduce
reduce已经取消了,如想使用,可以用fuctools.reduce来调用。但是要先导入fuctools, 即输入:import fuctools
4.lambda
快速定义最小单行函数
- g = lambda x:x *2
- print(g(5))
- print((lambda x:x *2)(5))
结果:
- 10
- 10
filter,map,reduce,lambda(python3)的更多相关文章
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- Map、Filter和Reduce函数(Python)
Map map(function_to_apply, list_of_inputs) 设有以下代码: >>> items = [1, 2, 3, 4, 5] >>> ...
- Python中特殊函数和表达式 filter,map,reduce,lambda
1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...
- 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)
Map/Reduce用户界面 本节为用户採用框架要面对的各个环节提供了具体的描写叙述,旨在与帮助用户对实现.配置和调优进行具体的设置.然而,开发时候还是要相应着API进行相关操作. 首先我们须要了解M ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- 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 ...
- lambda,filter,map,reduce
# lambda,filter,map,reduce from functools import reduce print('返回一个迭代器') print((x) for x in range(5) ...
随机推荐
- SOAP-XML请求(iOS应用下集成携程api)
用携程机票为例: 携程联盟 飞机票.门票 联盟ID:278639 站点ID:739462 密钥KEY:BE57B925-E8CE-4AA2-AC8E-3EE4BBBB686F API_URL:open ...
- offsetParent详解
offsetParent与parentNode一样,都是获取父节点,但是offsetParent却有很大的不同之处: offsetParent找有定位的父节点,没有定位默认是body,ie7以下定位在 ...
- JS中new都是干了些什么事情
var Person = function(name){ this.name = name; this.say = function(){ return "I am " + thi ...
- Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. ...
- 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参
转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...
- 用C++11的std::async代替线程的创建
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...
- Java的性能优化
http://www.toutiao.com/i6368345864624144897/?tt_from=mobile_qq&utm_campaign=client_share&app ...
- Openwrt dnsmasq 设置要点
之前设置dnsmasq,一直没有奏效,后来摸索了一下,初步发现它的原理: 正常的流程应该是像这样的,先由client来发送DNS请求到网关,然后网关的dnsmasq处理这个请求, 再根据设置决定如何处 ...
- Apache POI 实现对 Excel 文件读写
1. Apache POI 简介 Apache POI是Apache软件基金会的开放源码函式库. 提供API给Java应用程序对Microsoft Office格式档案读和写的功能. 老外起名字总是很 ...
- WPF下制作的简单瀑布流效果
最近又在搞点小东西,美化界面的时候发现瀑布流效果比较不错.顺便就搬到了WPF,下面是界面 我对WEB前端不熟,JS和CSS怎么实现的,我没去研究过,这里就说下WPF的实现思路,相当简单. 1.最重要的 ...