map

map函数根据提供的函数对指定的序列做映射,定义:
map(function, sequence[,sequence,...])--->list

例1

>>> map(lambda x:x+2, [1, 2, 3])
[3, 4, 5]
>>> map(lambda x:x+2, (1, 2, 3))
[3, 4, 5]
>>> map(lambda x:x+2, [1, 2], [1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (2 given)

最后这个例子说lambda函数需要传2个参数(因为后面的列表是2个)

例2

>>> map(lambda x,y:x+y, [1, 2], [1, 2])
[2, 4]
>>> map(lambda x,y:x+y, [1, 2], (1,2))
[2, 4]

例3

>>> a
[{'type': 2, 'ID': 1}, {'type': 4, 'ID': 2}, {'ID': 3}]
>>> map(lambda x:x['ID'], a)
[1, 2, 3]
>>> map(lambda x:x['type'], a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
KeyError: 'type'

例子说明,如果其中的一个键不存在({'ID':3}不存在type)会报错。

例4

上面例子中只给了lambda,还可以用普通的函数

>>> def func2(x, y):
... return x+y
...
>>> map(func2, [1, 2, 3], [3, 2, 1])
[4, 4, 4]
>>>
>>> def func1(x):
... return x**2
...
>>> map(func1, [1, 2, 3])
[1, 4, 9]

例5

如果没有给定,就类似于zip函数了

>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4])
[(1, 1), (2, 2), (3, 3), (4, 4)]
>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4,5])
[(1, 1), (2, 2), (3, 3), (4, 4), (None, 5)]
>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4,5], [1, 2, 3])
[(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, None), (None, 5, None)]

不过与zip不同

>>> zip([1, 2, 3, 4], [1, 2, 3, 4,5], [1, 2, 3])
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

filter

filter函数对指定的序列进行过滤操作。定义:

filter(function or None, sequence) -> list, tuple, or string

例1

>>> filter(lambda x:x%2==1, [1, 2, 3])
[1, 3]
>>> filter(lambda x:x%2==1, (1, 2, 3))
(1, 3)

reduce

reduce函数会对参数序列中元素进行累积。定义:
reduce(function, sequence[, initial]) -> value

:function必须是有2个参数的函数

例1

>>> reduce(lambda x, y:x+y, [1,2,3,4])
10
>>> reduce(lambda x, y:x+y, [1,2,3,4], 10)
20

如果没有initial参数,这么算:(((1+2)+3)+4)

如果有initial参数,这么算: ((((10+1)+2)+3)+4)

lambda

编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数

举例对比(列表中的元素平方):

>>> map(lambda x:x*x, range(5))
[0, 1, 4, 9, 16] >>> def sq(x):
... return x * x
...
>>> map(sq, range(5))
[0, 1, 4, 9, 16]
前者比后者好。因为后者多定义了一个(污染环境的)函数,尤其如果这个函数只会使用一次的话。而且第一种写法实际上更易读,因为那个映射到列表上的函数具体是要做什么,非常一目了然。如果你仔细观察自己的代码,会发现这种场景其实很常见:你在某处就真的只需要一个能做一件事情的函数而已,连它叫什么名字都无关紧要。Lambda 表达式就可以用来做这件事。

map(lambda x:x*x, range(5))
这样的写法时,你会发现自己如果能将「遍历列表,给遇到的每个元素都做某种运算」的过程从一个循环里抽象出来成为一个函数 map,然后用 lambda 表达式将这种运算作为参数传给 map 的话,考虑事情的思维层级会高出一些来,需要顾及的细节也少了一点。Python 之中,类似能用到 lambda 表达式的「高级」函数还有 reduce、filter 等等

python之map、filter、reduce、lambda函数的更多相关文章

  1. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  2. python几个特别函数map filter reduce lambda

    lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x): return x**2 print f(4) Python中使用lambda的话,写成这样 g = l ...

  3. Python中map,filter,reduce,zip的应用

    事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...

  4. Python【map、reduce、filter】内置函数使用说明(转载)

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  5. 【转】Python 中map、reduce、filter函数

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  6. Python【map、reduce、filter】内置函数使用说明

    题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...

  7. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  8. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  9. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  10. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

随机推荐

  1. HTML5 :b/strong加粗,i/em倾斜区别

    解释1 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="ut ...

  2. MyBatis传入参数为集合 list 数组 map写法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

  3. 省略nslog打印

    //#if #endif宏定义的意思就是如果定义了DEBUG,那么就使用NSLog输出:否则这段代码直接忽略.有人会疑问这个DEBUG和_DEBUG来自哪里,这个其实不用担心,这个来自于Xcode的默 ...

  4. Unity3d中C#使用指针(Unsafe)的办法(转)

    近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...

  5. 页面静态化1 --- 概念(Apache内置压力测试工具使用方法)

    三个概念 ①静态网址: http://127.0.0.1/regis.html ②动态网址:在请求页面时可以动态的传一些值进去. http://127.0.0.1/regis.php?id=45&am ...

  6. Apache(Web)服务器性能调整

    Apache多路处理模块---MPM apache通过不同的MPM运行在多进程和多线程混合模式下,增强配置扩充性能.MPM无法模块化,只能在编译配置时选择,被静态编译到服务器中.目前apache版本中 ...

  7. go access database demo

    package main import ( "database/sql" "fmt" _ "github.com/lib/pq" " ...

  8. eclipse Maven -->web project

    http://blog.chinaunix.net/uid-26959955-id-3248053.html http://blog.csdn.net/wilsonke/article/details ...

  9. Redis学习笔记(4)-List

    package cn.com; import java.util.List; import redis.clients.jedis.Jedis; import redis.clients.jedis. ...

  10. zepto源码--插入节点--学习笔记

    与生成width和height使用的方法类似,通过`after`, `prepend`, `before`, `append`,这四者之间的共性,生成对应的函数.并根据这四个函数,生成 `insert ...