1.filter

filter(function,sequence)

对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个list、string、tuple(根据sequence类型决定)返回。

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. @author: 侠之大者kamil
  5. @file: filter.py
  6. @time: 2016/4/9 22:03
  7. """
  8. #filter map reduce lambda
  9. def f1(x):
  10. return x % 2 != 0 and x % 3 != 0
  11. a = filter(f1, range(2,25))
  12. print(a)#<filter object at 0x7f7ee44823c8>
  13. #这种情况是因为在3.3里面,filter()的返回值已经不再是list,而是iterators.
  14. print(list(a))
  15. def f2(x):
  16. return x != "a"
  17. print(list(filter(f2,"dfsafdrea")))

结果:

  1. ssh://kamil@192.168.16.128:22/usr/bin/python3 -u /home/kamil/py22/jiqiao0406/fmrl.py
  2. <filter object at 0x7f4d1ca5e470>
  3. [5, 7, 11, 13, 17, 19, 23]
  4. ['d', 'f', 's', 'f', 'd', 'r', 'e']
  5.  
  6. Process finished with exit code 0

2.map

语法与filter类似

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. @author: 侠之大者kamil
  5. @file: map.py
  6. @time: 2016/4/9 22:22
  7. """
  8. def cube(x):
  9. return x * x *x
  10. a = map(cube,range(10))
  11. print(list(a))
  12. def add(x,y,z):
  13. return x + y +z
  14. b = map(add,range(5),range(5),range(5))
  15. print(list(b))

结果:

  1. ssh://kamil@192.168.16.128:22/usr/bin/python3 -u /home/kamil/py22/jiqiao0406/map.py
  2. [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
  3. [0, 3, 6, 9, 12]
  4.  
  5. Process finished with exit code 0

3.reduce

reduce已经取消了,如想使用,可以用fuctools.reduce来调用。但是要先导入fuctools, 即输入:import fuctools

4.lambda

快速定义最小单行函数

  1. g = lambda x:x *2
  2. print(g(5))
  3. print((lambda x:x *2)(5))

结果:

  1. 10
  2. 10

filter,map,reduce,lambda(python3)的更多相关文章

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

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

  2. Map、Filter和Reduce函数(Python)

    Map map(function_to_apply, list_of_inputs) 设有以下代码: >>> items = [1, 2, 3, 4, 5] >>> ...

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

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

  4. 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)

    Map/Reduce用户界面 本节为用户採用框架要面对的各个环节提供了具体的描写叙述,旨在与帮助用户对实现.配置和调优进行具体的设置.然而,开发时候还是要相应着API进行相关操作. 首先我们须要了解M ...

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

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

  6. python: filter, map, reduce, lambda

    filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...

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

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

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

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

  9. lambda,filter,map,reduce

    # lambda,filter,map,reduce from functools import reduce print('返回一个迭代器') print((x) for x in range(5) ...

随机推荐

  1. SOAP-XML请求(iOS应用下集成携程api)

    用携程机票为例: 携程联盟 飞机票.门票 联盟ID:278639 站点ID:739462 密钥KEY:BE57B925-E8CE-4AA2-AC8E-3EE4BBBB686F API_URL:open ...

  2. offsetParent详解

    offsetParent与parentNode一样,都是获取父节点,但是offsetParent却有很大的不同之处: offsetParent找有定位的父节点,没有定位默认是body,ie7以下定位在 ...

  3. JS中new都是干了些什么事情

    var Person = function(name){ this.name = name; this.say = function(){ return "I am " + thi ...

  4. 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. ...

  5. 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参

    转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...

  6. 用C++11的std::async代替线程的创建

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...

  7. Java的性能优化

    http://www.toutiao.com/i6368345864624144897/?tt_from=mobile_qq&utm_campaign=client_share&app ...

  8. Openwrt dnsmasq 设置要点

    之前设置dnsmasq,一直没有奏效,后来摸索了一下,初步发现它的原理: 正常的流程应该是像这样的,先由client来发送DNS请求到网关,然后网关的dnsmasq处理这个请求, 再根据设置决定如何处 ...

  9. Apache POI 实现对 Excel 文件读写

    1. Apache POI 简介 Apache POI是Apache软件基金会的开放源码函式库. 提供API给Java应用程序对Microsoft Office格式档案读和写的功能. 老外起名字总是很 ...

  10. WPF下制作的简单瀑布流效果

    最近又在搞点小东西,美化界面的时候发现瀑布流效果比较不错.顺便就搬到了WPF,下面是界面 我对WEB前端不熟,JS和CSS怎么实现的,我没去研究过,这里就说下WPF的实现思路,相当简单. 1.最重要的 ...