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. 在Windows2003上安装Active Directory Management Gateway Service

    为了让基于Windows2003的域控能够被Powershell管理,必须安装KB968934,但是直接安装会报以下的错误,必须先安装NDP35SP1-KB969166.但是"因为基于 Wi ...

  2. 【iHMI43 4.3寸液晶模块】demo例程(版本1.02)发布

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  3. windows下Ruby开发环境搭建

    一.下载 下载RubyInstaller 二.安装 按照提示安装 安装完毕后,在cmd命令窗口,输入:ruby -v 查询rbuy版本:输入gem -v 查询gem版本 三.Ruby插件(Redis插 ...

  4. 2016.03.31,英语,《Vocabulary Builder》Unit 08

    tend/tent: from the Latin tendere, meaning 'to stretch, extend, or spread'. tent: [tent] n. 帐篷 vt.&a ...

  5. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [2] 首页 APP 接口开发方案 ① 读取数据库方式

    方案一:读取数据库方式 从数据库读取信息→封装→生成接口数据 应用场景: 数据时效性比较高的系统 方案二:读取缓存方式 从数据库获取信息(第一次设置缓存或缓存失效时)→封装(第一次设置缓存或缓存失效时 ...

  6. 微信的User-Agent

    Mozilla/5.0 (Linux; U; Android 5.0.2; zh-cn; MI 2C Build/LRX22G) AppleWebKit/533.1 (KHTML, like Geck ...

  7. 总结下遇到的C#新语法

    1.属性的private set读写器 public int x { get; private set; } 是另一种简写, set前面的private声明是告诉编译器属性X是只读(read-only ...

  8. NRF51822之发射功率

    设置蓝牙的TX Power 使用的函数sd_ble_gap_tx_power_set(int8_t tx_power);   参看例子为 S110/ble_app_proximity #define ...

  9. 自动将每日的日志增量导入到hive中

    一:大纲介绍 1.导入方式 load data local inpath 'local_file_path' into table tbname partition (date='',hour='') ...

  10. Unix网络编程(迭代服务器)

    #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...