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)的更多相关文章

  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. CoreAnimation笔记

    核心动画继承结构 CoreAnimation Core Animation是直接作用在CALayer上的(并非UIView上)非常强大的跨Mac OS X和iOS平台的动画处理API,Core Ani ...

  2. ListView实现原理

    转载:http://blog.csdn.net/guolin_blog/article/details/44996879 在Android所有常用的原生控件当中,用法最复杂的应该就是ListView了 ...

  3. [转]源代码的管理和发布:以SVN为例

    FROM : http://ju.outofmemory.cn/entry/47277 前几天在微博吐槽了SVN的几个不爽的地方:.svn文件满天飞.分支管理的麻烦.不爽一般来说都是有过对比后才有如此 ...

  4. [转]Linux后台进程管理利器:supervisor

    FROM : http://www.liaoxuefeng.com/article/0013738926914703df5e93589a14c19807f0e285194fe84000 Linux后台 ...

  5. noi1696 逆波兰表达式

    1696:逆波兰表达式 http://noi.openjudge.cn/ch0303/1696/ 总时间限制:  1000ms 内存限制:  65536kB 描述 逆波兰表达式是一种把运算符前置的算术 ...

  6. swift——启动页国际化:一步一步动态加载启动页图片,启动的时候加载文字

    由于公司的需求,要求做一个国际化的启动页,因为app我也弄国际化了,就剩下启动页国际化未完成,百度了呵谷歌了好多答案都不尽如人意,最后也是看见同事完成,我也问了具体的做法,决定分享给需要的人,免得和我 ...

  7. PhoneGap: Android 自定义组件

    Hello Core Demo Plugin Development(组件部署): http://docs.phonegap.com/en/2.0.0/guide_plugin-development ...

  8. c#新语法学习笔记

    1.匿名类 匿名类编译之后会生成一个具体的泛型类,匿名类的属性是只读的.在临时数据传递时非常方便(linq查询).匿名类中不能有方法.数据传输(json),数据查询(linq) }; 2.匿名方法匿名 ...

  9. [c++]printf的编译器静态检测

    经常会用到C的格式化字符串,gcc/clang的扩展,都是可以进行字符串和可变参数的检测,不匹配,就会爆warning. 自己手写的,需要加上一点代码,否则不会进行检测. 1.  可变参数 void ...

  10. 东大oj-1591 Circle of friends

    题目描述 Nowadays, "Circle of Friends" is a very popular social networking platform in WeChat. ...